Skip to content

Commit 07f0af0

Browse files
committed
Add JavaScript-based redirects to supplement Read the Docs redirects
Read the Docs redirects don't reliably apply when there is a large amount of redirects defined. While this JavaScript-based solution is not as seamless, it has no limit on how many redirects can be defined.
1 parent e958d86 commit 07f0af0

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

_static/js/custom.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
// Handle page scroll and adjust sidebar accordingly.
32

43
// Each page has two scrolls: the main scroll, which is moving the content of the page;
@@ -298,6 +297,37 @@ const registerGiscus = function () {
298297
};
299298

300299
$(document).ready(() => {
300+
const httpResponseStatus = window.performance.getEntries()[0].responseStatus;
301+
if (httpResponseStatus === 404) {
302+
// Check for redirects if on a currently invalid page.
303+
// This is done in JavaScript, as Read the Docs' own redirect functionality
304+
// is unreliable with large amounts of redirects defined.
305+
fetch("https://raw.githubusercontent.com/godotengine/godot-docs/refs/heads/master/_tools/redirects/redirects.csv")
306+
.then(response => response.text())
307+
.then(csvText => {
308+
const lines = csvText.trim().split('\n');
309+
const redirects = {};
310+
for (const line of lines) {
311+
if (!line.trim()) {
312+
continue;
313+
}
314+
const [from, to] = line.split(',').map(s => s.trim());
315+
if (from && to) {
316+
redirects[from] = to;
317+
}
318+
}
319+
320+
if (redirects.hasOwnProperty(window.location.pathname)) {
321+
const newUrl = window.location.href.replace(window.location.pathname, redirects[window.location.pathname]);
322+
console.log(`Redirecting to: ${newUrl}`);
323+
window.location.replace(newUrl);
324+
}
325+
})
326+
.catch(err => {
327+
console.error("Couldn't fetch redirects list:", err);
328+
});
329+
}
330+
301331
// Remove the search match highlights from the page, and adjust the URL in the
302332
// navigation history.
303333
const url = new URL(location.href);

0 commit comments

Comments
 (0)