@@ -253,22 +253,63 @@ $(document).ready(() => {
253
253
if ( inDev ) {
254
254
// Add a compatibility notice using JavaScript so it doesn't end up in the
255
255
// automatically generated `meta description` tag.
256
- const strippedUrl = [ location . protocol , '//' , location . host , location . pathname ] . join ( '' ) ;
257
- const updatedUrl = strippedUrl . replace ( '/latest/' , '/stable/' ) ;
256
+
257
+ const baseUrl = [ location . protocol , '//' , location . host , location . pathname ] . join ( '' ) ;
258
+ // These lines only work as expected in the production environment, can't test this locally.
259
+ const fallbackUrl = baseUrl . replace ( '/latest/' , '/stable/' ) ;
260
+ const homeUrl = baseUrl . split ( '/latest/' ) [ 0 ] + '/stable/' ;
261
+ const searchUrl = homeUrl + 'search.html?q=' ;
262
+
263
+ // Insert the base notice with a placeholder to display as we're making a request.
258
264
document . querySelector ( 'div[itemprop="articleBody"]' ) . insertAdjacentHTML ( 'afterbegin' , `
259
- <div class="admonition attention">
265
+ <div class="admonition attention latest-notice ">
260
266
<p class="first admonition-title">Attention</p>
261
267
<p>
262
268
You are reading the <code class="docutils literal notranslate"><span class="pre">latest</span></code>
263
269
(unstable) version of this documentation, which may document features not available
264
270
or compatible with Godot 3.x.
265
271
</p>
266
- <p class="last">
267
- See <a class="reference" href="${ updatedUrl } ">this page </a>
268
- for the stable version of this documentation.
272
+ <p class="last latest-notice-link ">
273
+ Checking the <a class="reference" href="${ homeUrl } ">stable version </a>
274
+ of the documentation.. .
269
275
</p>
270
276
</div>
271
277
` ) ;
278
+
279
+ const noticeLink = document . querySelector ( '.latest-notice-link' ) ;
280
+
281
+ // Make a HEAD request to the possible stable URL to check if the page exists.
282
+ fetch ( fallbackUrl , { method : 'HEAD' } )
283
+ . then ( ( res ) => {
284
+ // We only check the HTTP status, which should tell us if the link is valid or not.
285
+ if ( res . status === 200 ) {
286
+ noticeLink . innerHTML = `
287
+ See the <a class="reference" href="${ fallbackUrl } ">stable version</a>
288
+ of this documentation page instead.
289
+ ` ;
290
+ } else {
291
+ // Err, just to fallthrough to catch.
292
+ throw Error ( 'Bad request' ) ;
293
+ }
294
+ } )
295
+ . catch ( ( err ) => {
296
+ let message = `
297
+ This page does not exist in the <a class="reference" href="${ homeUrl } ">stable version</a>
298
+ of the documentation.
299
+ ` ;
300
+
301
+ // Also suggest a search query using the page's title. It should work with translations as well.
302
+ // Note that we can't use the title tag as it has a permanent suffix. OG title doesn't, though.
303
+ const titleMeta = document . querySelector ( 'meta[property="og:title"]' ) ;
304
+ if ( typeof titleMeta !== 'undefined' ) {
305
+ const pageTitle = titleMeta . getAttribute ( 'content' ) ;
306
+ message += `
307
+ You can try searching for "<a class="reference" href="${ searchUrl + encodeURIComponent ( pageTitle ) } ">${ pageTitle } </a>" instead.
308
+ ` ;
309
+ }
310
+
311
+ noticeLink . innerHTML = message ;
312
+ } ) ;
272
313
}
273
314
274
315
// Load instant.page to prefetch pages upon hovering. This makes navigation feel
0 commit comments