From 093acc43c67c079a777a49954d8190a37215480c Mon Sep 17 00:00:00 2001 From: Yuri Sizov Date: Mon, 5 Dec 2022 03:48:50 +0300 Subject: [PATCH] Dynamically adjust the compatibility notice in the latest docs --- _static/js/custom.js | 53 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/_static/js/custom.js b/_static/js/custom.js index b55c14f59..7e6d51d4d 100644 --- a/_static/js/custom.js +++ b/_static/js/custom.js @@ -253,22 +253,63 @@ $(document).ready(() => { if (inDev) { // Add a compatibility notice using JavaScript so it doesn't end up in the // automatically generated `meta description` tag. - const strippedUrl = [location.protocol, '//', location.host, location.pathname].join(''); - const updatedUrl = strippedUrl.replace('/latest/', '/stable/'); + + const baseUrl = [location.protocol, '//', location.host, location.pathname].join(''); + // These lines only work as expected in the production environment, can't test this locally. + const fallbackUrl = baseUrl.replace('/latest/', '/stable/'); + const homeUrl = baseUrl.split('/latest/')[0] + '/stable/'; + const searchUrl = homeUrl + 'search.html?q='; + + // Insert the base notice with a placeholder to display as we're making a request. document.querySelector('div[itemprop="articleBody"]').insertAdjacentHTML('afterbegin', ` -
+

Attention

You are reading the latest (unstable) version of this documentation, which may document features not available or compatible with Godot 3.x.

-

- See this page - for the stable version of this documentation. +

`); + + const noticeLink = document.querySelector('.latest-notice-link'); + + // Make a HEAD request to the possible stable URL to check if the page exists. + fetch(fallbackUrl, { method: 'HEAD' }) + .then((res) => { + // We only check the HTTP status, which should tell us if the link is valid or not. + if (res.status === 200) { + noticeLink.innerHTML = ` + See the stable version + of this documentation page instead. + `; + } else { + // Err, just to fallthrough to catch. + throw Error('Bad request'); + } + }) + .catch((err) => { + let message = ` + This page does not exist in the stable version + of the documentation. + `; + + // Also suggest a search query using the page's title. It should work with translations as well. + // Note that we can't use the title tag as it has a permanent suffix. OG title doesn't, though. + const titleMeta = document.querySelector('meta[property="og:title"]'); + if (typeof titleMeta !== 'undefined') { + const pageTitle = titleMeta.getAttribute('content'); + message += ` + You can try searching for "${pageTitle}" instead. + `; + } + + noticeLink.innerHTML = message; + }); } // Load instant.page to prefetch pages upon hovering. This makes navigation feel