From aa0a55ab4f029392bb83382b4e1b95362a7728ee Mon Sep 17 00:00:00 2001 From: Daniel McCloy Date: Wed, 26 Jul 2023 16:37:46 -0500 Subject: [PATCH] actually respond to the header status code --- .../assets/scripts/pydata-sphinx-theme.js | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js b/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js index 746f040ae..0c24d2c46 100644 --- a/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js +++ b/src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js @@ -287,21 +287,23 @@ var setupSearchButtons = () => { * * @param {event} event the event that trigger the check */ -function checkPageExistsAndRedirect(event) { +async function checkPageExistsAndRedirect(event) { // ensure we don't follow the initial link event.preventDefault(); - - const currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`; - const tryUrl = event.currentTarget.getAttribute("href"); + let currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`; + let tryUrl = event.currentTarget.getAttribute("href"); let otherDocsHomepage = tryUrl.replace(currentFilePath, ""); - - fetch(tryUrl, { method: "HEAD" }) - .then(() => { - location.href = tryUrl; - }) // if the page exists, go there - .catch((error) => { + try { + let head = await fetch(tryUrl, { method: "HEAD" }); + if (head.ok) { + location.href = tryUrl; // the page exists, go there + } else { location.href = otherDocsHomepage; - }); + } + } catch (err) { + // something went wrong, probably CORS restriction, fallback to other docs homepage + location.href = otherDocsHomepage; + } } /**