Skip to content

Commit 093acc4

Browse files
committed
Dynamically adjust the compatibility notice in the latest docs
1 parent 346c4d5 commit 093acc4

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

_static/js/custom.js

+47-6
Original file line numberDiff line numberDiff line change
@@ -253,22 +253,63 @@ $(document).ready(() => {
253253
if (inDev) {
254254
// Add a compatibility notice using JavaScript so it doesn't end up in the
255255
// 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.
258264
document.querySelector('div[itemprop="articleBody"]').insertAdjacentHTML('afterbegin', `
259-
<div class="admonition attention">
265+
<div class="admonition attention latest-notice">
260266
<p class="first admonition-title">Attention</p>
261267
<p>
262268
You are reading the <code class="docutils literal notranslate"><span class="pre">latest</span></code>
263269
(unstable) version of this documentation, which may document features not available
264270
or compatible with Godot 3.x.
265271
</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...
269275
</p>
270276
</div>
271277
`);
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+
});
272313
}
273314

274315
// Load instant.page to prefetch pages upon hovering. This makes navigation feel

0 commit comments

Comments
 (0)