-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ec5164
commit 7c90339
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |