Skip to content

Commit c610f9a

Browse files
authored
Fix redirection for pages missing in other docs versions (#1395)
actually respond to the header status code
1 parent 66a826c commit c610f9a

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -287,21 +287,23 @@ var setupSearchButtons = () => {
287287
*
288288
* @param {event} event the event that trigger the check
289289
*/
290-
function checkPageExistsAndRedirect(event) {
290+
async function checkPageExistsAndRedirect(event) {
291291
// ensure we don't follow the initial link
292292
event.preventDefault();
293-
294-
const currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`;
295-
const tryUrl = event.currentTarget.getAttribute("href");
293+
let currentFilePath = `${DOCUMENTATION_OPTIONS.pagename}.html`;
294+
let tryUrl = event.currentTarget.getAttribute("href");
296295
let otherDocsHomepage = tryUrl.replace(currentFilePath, "");
297-
298-
fetch(tryUrl, { method: "HEAD" })
299-
.then(() => {
300-
location.href = tryUrl;
301-
}) // if the page exists, go there
302-
.catch((error) => {
296+
try {
297+
let head = await fetch(tryUrl, { method: "HEAD" });
298+
if (head.ok) {
299+
location.href = tryUrl; // the page exists, go there
300+
} else {
303301
location.href = otherDocsHomepage;
304-
});
302+
}
303+
} catch (err) {
304+
// something went wrong, probably CORS restriction, fallback to other docs homepage
305+
location.href = otherDocsHomepage;
306+
}
305307
}
306308

307309
/**

0 commit comments

Comments
 (0)