Skip to content
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ additional-css = [
site-url = "/comprehensive-rust/"
git-repository-url = "https://github.com/google/comprehensive-rust"
edit-url-template = "https://github.com/google/comprehensive-rust/edit/main/{path}"
input-404 = "404.md"

[output.html.fold]
enable = true
Expand Down
39 changes: 39 additions & 0 deletions src/404.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Document not found (404)

This URL is invalid, sorry. Please use the navigation bar or search to continue.

<script>
(function () {
const baseEl = document.querySelector("base");
const baseHref = baseEl ? baseEl.getAttribute("href") || "/" : "/";
const knownLangs = new Set(
Array.from(document.querySelectorAll("#language-list a[id]"), (link) => link.id),
);

const basePath = new URL(baseHref, window.location.origin).pathname;

const fullPath = window.location.pathname;

let relPath = fullPath.startsWith(basePath)
? fullPath.slice(basePath.length)
: fullPath.replace(/^\/+/, "");

if (!relPath) {
return;
}

const segments = relPath.split("/");
const maybeLang = segments[0];

if (!knownLangs.has(maybeLang) || maybeLang === "en") {
return;
}

if (relPath === maybeLang + "/404.html" || relPath === "404.html") {
return;
}

const targetUrl = new URL(maybeLang + "/404.html", baseHref).toString();
window.location.replace(targetUrl);
})();
</script>
Loading