Skip to content

Commit

Permalink
Create 404.html with redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
avanwinkle authored Aug 16, 2024
1 parent 2ec5164 commit 7c90339
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions 404.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<script>
// Check if the current path is versioned, if not, redirect to the default versioned path
const versions = ["0.57", "0.80", "latest", "dev"]
const defaultVersion = "latest"
const targetRedirectPath = "404" // path to redirect to

// if path starts with version, redirect to versioned 404
let foundVersion = false
versions.forEach(version => {
const path = window.location.pathname;
if (path.startsWith(`/${version}`) && !path.startsWith(`/${version}/`)) {
const versionToReplace = path.split('/')[1];
foundVersion = true;
window.location.href = window.location.href.replace(versionToReplace, version);
} else if (path.startsWith(`/${version}/`)) {
// we need this foundVersion guard because the browser is fast and
// will keep the executing code below until the redirect happens
foundVersion = true;
window.location.href = `/${version}/${targetRedirectPath}`;
}
});

// if path doesn't start with any version, redirect to defaultVersion
// Replace it in href, so we keep hashes and query params
if (!foundVersion){
const pathParts = window.location.pathname.split('/');
if (pathParts.length >= 2) {
const stringToReplace = pathParts[1];
window.location.href = window.location.href.replace(stringToReplace, defaultVersion);
} else {
window.location.href = `/${defaultVersion}/${targetRedirectPath}`;
}
}
</script>

0 comments on commit 7c90339

Please sign in to comment.