feat/site: Improve 404 page with links back to the previous page and up to the nearest ancestor - #1887
Open
marcleblanc2 wants to merge 3 commits into
Open
feat/site: Improve 404 page with links back to the previous page and up to the nearest ancestor#1887marcleblanc2 wants to merge 3 commits into
marcleblanc2 wants to merge 3 commits into
Conversation
…ing ancestor The 404 page only offered a link home. Now it also shows: - "Go back to <path>" when document.referrer is on the same origin - "Go up to <path>" for the closest ancestor of the requested URL that is a real docs page (preview pages excluded), when that is not the root not-found.tsx stays a server component and passes the list of page paths from contentlayer to a small client component, which resolves both links after mount because the prerendered 404 page does not know the browser URL. Amp-Thread-ID: https://ampcode.com/threads/T-01a08450-afc0-7264-afde-d3e2a47be6d2 Co-authored-by: Amp <amp@ampcode.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
…ent-side navigation document.referrer only reflects the last full page load, so clicking a broken <Link> inside the docs left the 404 page without a back link. A PreviousPathnameProvider in the root layout (which React keeps mounted across client-side navigations) now remembers the prior pathname; the 404 page prefers it and falls back to a same-origin document.referrer on fresh loads. Amp-Thread-ID: https://ampcode.com/threads/T-01a08450-afc0-7264-afde-d3e2a47be6d2 Co-authored-by: Amp <amp@ampcode.com>
Editing the URL bar is a fresh load with no referrer, so the 404 page had nothing to link back to. Persist the visited pathnames in sessionStorage (per tab) so that case works too, and only offer "Go back" when the previous pathname is a real page, since it may itself have been a 404.
marcleblanc2
marked this pull request as ready for review
September 9, 2026 10:28
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The 404 page only offered "Go back home". It now also shows, when applicable:
<path>— the docs page the user was on before this one<path>— the closest ancestor of the requested URL that is a real docs page (preview pages excluded). Omitted when that ancestor is the root, since the home link already covers it.How
src/components/PreviousPathname.tsx: aPreviousPathnameProvidermounted insrc/app/providers.tsx(root layout, so React keeps it alive across client-side navigations) remembers the previoususePathname()value, persisted insessionStorage(per tab) so it also survives full loads such as editing the URL bar, which sends no referrer.src/components/NotFoundLinks.tsx(client) renders the links. "Go back" uses the previous pathname when it is a real page (it may itself have been a 404), and falls back to a same-origindocument.referreron fresh loads (e.g. arriving fromsourcegraph.com/search). Cross-origin referrers are never linked.src/app/not-found.tsxstays a server component and passesallPostsURLs (~520 short strings) toNotFoundLinks, which resolves everything after mount because the statically prerendered 404 page does not know the browser URL.Notes
/admin/access-control/a/bstill offers/admin/access-control. Middleware redirects (e.g./admin/deploy) are not pages and are skipped.Verified
Headless Chrome against
pnpm dev(temporary broken[link](...)added to a doc page after startup, not committed, so the Next router does the navigation):/code-search/queries, edit URL bar to/code-search/queries/nope(no referrer)/code-search/queries, Go up to/code-search/queries, home/code-search/nope2/code-search, home (first 404 is not offered as "back")<Link>click from/admin/audit-log→ broken (0 full loads)/admin/audit-log, Go up to/admin/executors/executor-secrets, home/admin/audit-log<Link>hops audit-log → access-control → broken/admin/access-control(not audit-log), Go up, home/code-search/queries/nope/code-search/queries, home/totally-missingnpx tsc --noEmitclean;pnpm lintonly pre-existingno-img-elementwarnings.