-
Notifications
You must be signed in to change notification settings - Fork 47
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
Provide a 404 page #72
Comments
moreover, would be nice to have something it redirect to the versioned page... e.g., I go to mysite.com/subpage, it gets redirected to mysite.com/latest/subpage - that way its easier to migrate from not having versioned docs... |
Hi, thanks for creating Mike! I’m looking to adopt it for gitlint (very recently adopted Material for Mkdocs, after using vanilla mkdocs for many years).
This is something I’d like to have, to avoid breaking old URLs. I’d happily help implement this if you’re accepting PRs for it? If so, could use a pointer on how to get started. Thanks! |
It shouldn't actually be necessary for mike to do anything particular here: all you need to do is add your own
This probably means writing some JS to do that, which is annoying since you can't unconditionally redirect or else you could end up in a redirect loop. |
A further limitation to putting this in mike is that mike currently doesn't record the default-version in a place that's easy to parse back out, so we'd need some kind of solution there (though that could be "generate Speaking of |
Got it, thanks! Let me give this a try :-) |
Ok, I gave this an attempt, adding the <script>
// Check if the current path is versioned, if not, redirect to the default versioned path
const versions = ["0.19.x", "latest"]
const defaultVersion = "latest"
const basePath = "/gitlint"
const targetRedirectPath = "404" // path to redirect to, relative to basePath
// if path starts with version, redirect to versioned 404
let foundVersion = false
versions.forEach(version => {
const versionedPath = `${basePath}/${version}`;
if (window.location.pathname.startsWith(versionedPath)) {
// 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 = `${versionedPath}/${targetRedirectPath}`
}
});
// if path doesn't start with any version, redirect to defaultVersion
// Replace it in href, so we keep hashes and query params
// Only replace first occurence of basePath
if (!foundVersion){
window.location.href = window.location.href.replace(basePath, `${basePath}/${defaultVersion}`)
}
</script> If you’d want to make something like this part of mike, I think you indeed would need to generate this file with an While it might be possible to parse some the variables defined at the start from the active path rather than hardcoding (configuring) them, I think it gets tricky to figure out which parts of the URI are the Wrt the variables at the top:
|
Currently, there's no 404 page (since MkDocs'
404.html
ends up in a subdirectory). We should fix this and possibly include some basic logic to redirect users to the right page.The text was updated successfully, but these errors were encountered: