|
| 1 | +--- |
| 2 | +/** |
| 3 | + * Body of the 404 page (content/docs/404.mdx), which GitHub Pages serves for |
| 4 | + * every unknown path. |
| 5 | + * |
| 6 | + * Beyond the generic recovery links it rescues class lookups: a class page |
| 7 | + * exists only under its display name, so `/classes/<x>` 404s whenever `<x>` |
| 8 | + * is any other spelling of the same class - its hash once the name has been |
| 9 | + * resolved from the hash tables, a padded or unprefixed hash, or the name in |
| 10 | + * the wrong case. classHashes.json maps every canonical hash onto the slug |
| 11 | + * the page is really at, so those all resolve client-side and redirect. |
| 12 | + */ |
| 13 | +--- |
| 14 | + |
| 15 | +<div class="not-found"> |
| 16 | + <p class="nf-status" data-nf-status hidden aria-live="polite"></p> |
| 17 | + |
| 18 | + <div data-nf-generic> |
| 19 | + <p> |
| 20 | + That page doesn't exist. If you followed a link to a class, its name may |
| 21 | + have changed since - press <kbd>Ctrl</kbd> <kbd>K</kbd> to search every |
| 22 | + class and property on the site. |
| 23 | + </p> |
| 24 | + <p class="nf-links"> |
| 25 | + <a href="/classes/">Class reference</a> |
| 26 | + <a href="/changelog/">Patch changelog</a> |
| 27 | + <a href="/">Home</a> |
| 28 | + </p> |
| 29 | + </div> |
| 30 | +</div> |
| 31 | + |
| 32 | +<script> |
| 33 | + import type { ClassHashIndex } from "../types"; |
| 34 | + import { parseClassHash } from "../utils/classHash"; |
| 35 | + |
| 36 | + const base = import.meta.env.BASE_URL.replace(/\/$/, ""); |
| 37 | + const status = document.querySelector<HTMLElement>("[data-nf-status]"); |
| 38 | + const generic = document.querySelector<HTMLElement>("[data-nf-generic]"); |
| 39 | + |
| 40 | + /** "/classes/0x6516A/" → "0x6516A"; null when this isn't a class lookup. */ |
| 41 | + function classSegment(): string | null { |
| 42 | + const path = location.pathname.startsWith(base) |
| 43 | + ? location.pathname.slice(base.length) |
| 44 | + : location.pathname; |
| 45 | + // Case-insensitive: "/Classes/…" is a 404 too, and the redirect below |
| 46 | + // rebuilds the path from the lowercase form either way. |
| 47 | + const match = /^\/classes\/([^/]+)\/?$/i.exec(path); |
| 48 | + if (!match) return null; |
| 49 | + try { |
| 50 | + return decodeURIComponent(match[1]!); |
| 51 | + } catch { |
| 52 | + return match[1]!; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /** The slug this spelling of a class has a page at, if any. */ |
| 57 | + async function resolveSlug(segment: string): Promise<string | null> { |
| 58 | + const res = await fetch(`${base}/db/classHashes.json`); |
| 59 | + if (!res.ok) return null; |
| 60 | + const hashes: ClassHashIndex = await res.json(); |
| 61 | + // Right name or hash, wrong case: slugs are lowercase, and the index's |
| 62 | + // values are exactly the set of slugs a page exists at. |
| 63 | + const slugs = new Set(Object.values(hashes)); |
| 64 | + const lower = segment.toLowerCase(); |
| 65 | + if (slugs.has(lower)) return lower; |
| 66 | + // Otherwise a hash in some other spelling than the one the page is at. |
| 67 | + const hash = parseClassHash(segment); |
| 68 | + return (hash && hashes[hash]) || null; |
| 69 | + } |
| 70 | + |
| 71 | + const segment = classSegment(); |
| 72 | + if (segment && status && generic) { |
| 73 | + const label = document.createElement("code"); |
| 74 | + label.textContent = segment; |
| 75 | + status.replaceChildren("Looking for ", label, "…"); |
| 76 | + status.hidden = false; |
| 77 | + generic.hidden = true; |
| 78 | + |
| 79 | + resolveSlug(segment) |
| 80 | + .then((slug) => { |
| 81 | + const target = slug ? `${base}/classes/${slug}/` : null; |
| 82 | + // Never redirect onto the path we are already on: if the index knows |
| 83 | + // a slug whose page is missing (a deploy caught mid-flight), bouncing |
| 84 | + // to it would land back here and loop. |
| 85 | + if (target && target !== location.pathname) { |
| 86 | + // replace(), not assign(): Back should return to wherever the stale |
| 87 | + // link was, not to this page. Query and hash ride along - they may |
| 88 | + // carry a ?highlight= term or a property anchor. |
| 89 | + location.replace(target + location.search + location.hash); |
| 90 | + return; |
| 91 | + } |
| 92 | + status.replaceChildren("No class matches ", label, "."); |
| 93 | + generic.hidden = false; |
| 94 | + }) |
| 95 | + .catch(() => { |
| 96 | + status.hidden = true; |
| 97 | + generic.hidden = false; |
| 98 | + }); |
| 99 | + } |
| 100 | +</script> |
| 101 | + |
| 102 | +<style> |
| 103 | + .not-found { |
| 104 | + max-width: 45rem; |
| 105 | + margin: 0 auto; |
| 106 | + text-align: center; |
| 107 | + } |
| 108 | + |
| 109 | + .nf-status { |
| 110 | + font-size: var(--sl-text-body); |
| 111 | + color: var(--sl-color-gray-2); |
| 112 | + } |
| 113 | + |
| 114 | + .nf-status code { |
| 115 | + color: var(--sl-color-white); |
| 116 | + } |
| 117 | + |
| 118 | + .nf-links { |
| 119 | + display: flex; |
| 120 | + flex-wrap: wrap; |
| 121 | + justify-content: center; |
| 122 | + gap: 0.5rem 1.5rem; |
| 123 | + } |
| 124 | +</style> |
0 commit comments