Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix redirection for pages missing in other docs versions #1395

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand Down