@@ -26,6 +26,11 @@ const HIDE_TOOLTIP_DELAY = IS_TESTING ? 0 : 300;
2626
2727const TOOLTIP_DATA_HREF = "data-linkpreview-href" ;
2828
29+ // Object to save the responses from the EmbedAPI.
30+ // This way we don't have to perform multiple times the same request.
31+ // The key is the URL and the value is the JSON response.
32+ const cachedRemoteResponses = { } ;
33+
2934function setupTooltip ( el , doctoolname , doctoolversion , selector ) {
3035 // Take the provided element and setup the listeners required to
3136 // make the linkpreviews work
@@ -86,19 +91,31 @@ function setupTooltip(el, doctoolname, doctoolversion, selector) {
8691 if ( newTooltip !== undefined ) {
8792 const url = getEmbedURL ( anchorElement . href ) ;
8893
89- fetch ( url , {
90- method : "GET" ,
91- headers : {
92- "X-RTD-Hosting-Integrations-Version" : CLIENT_VERSION ,
93- } ,
94- } )
95- . then ( ( response ) => {
94+ let promiseData ;
95+ // Check if we have the response already cached
96+ if ( Object . keys ( cachedRemoteResponses ) . includes ( url ) ) {
97+ console . debug ( "URL was found cached, reusing it." ) ;
98+ promiseData = Promise . resolve ( cachedRemoteResponses [ url ] ) ;
99+ } else {
100+ console . debug ( "URL not found cached, performing a fetch." ) ;
101+ promiseData = fetch ( url , {
102+ method : "GET" ,
103+ headers : {
104+ "X-RTD-Hosting-Integrations-Version" : CLIENT_VERSION ,
105+ } ,
106+ } ) . then ( ( response ) => {
96107 if ( ! response . ok ) {
97108 throw new Error ( "Error hitting Read the Docs embed API" ) ;
98109 }
99110 return response . json ( ) ;
100- } )
111+ } ) ;
112+ }
113+
114+ promiseData
101115 . then ( ( data ) => {
116+ // Cache the response to use it later
117+ cachedRemoteResponses [ url ] = data ;
118+
102119 // TODO: decide whether or not to truncate the content
103120 // Do we want to have "modals" as well? are those useful?
104121 const content = data [ "content" ] ;
0 commit comments