Skip to content

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
mainfrom
not-found-back-and-up-links
Open

feat/site: Improve 404 page with links back to the previous page and up to the nearest ancestor#1887
marcleblanc2 wants to merge 3 commits into
mainfrom
not-found-back-and-up-links

Conversation

@marcleblanc2

@marcleblanc2 marcleblanc2 commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

What

The 404 page only offered "Go back home". It now also shows, when applicable:

  • Go back to <path> — the docs page the user was on before this one
  • Go up to <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: a PreviousPathnameProvider mounted in src/app/providers.tsx (root layout, so React keeps it alive across client-side navigations) remembers the previous usePathname() value, persisted in sessionStorage (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-origin document.referrer on fresh loads (e.g. arriving from sourcegraph.com/search). Cross-origin referrers are never linked.
  • src/app/not-found.tsx stays a server component and passes allPosts URLs (~520 short strings) to NotFoundLinks, which resolves everything after mount because the statically prerendered 404 page does not know the browser URL.

Notes

  • "Up" walks to the nearest existing ancestor rather than strictly one level, so /admin/access-control/a/b still offers /admin/access-control. Middleware redirects (e.g. /admin/deploy) are not pages and are skipped.
  • A brand-new tab with a typed URL has nothing to go back to, so only "Go up" and home appear.

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):

case links shown
on /code-search/queries, edit URL bar to /code-search/queries/nope (no referrer) Go back to /code-search/queries, Go up to /code-search/queries, home
reload that 404 same
then type a second broken URL /code-search/nope2 Go up to /code-search, home (first 404 is not offered as "back")
<Link> click from /admin/audit-log → broken (0 full loads) Go back to /admin/audit-log, Go up to /admin/executors/executor-secrets, home
clicking "Go back to" lands on /admin/audit-log
two <Link> hops audit-log → access-control → broken Go back to /admin/access-control (not audit-log), Go up, home
full load with same-origin referrer, empty storage Go back via referrer fallback, Go up, home
new tab, typed /code-search/queries/nope Go up to /code-search/queries, home
new tab, typed /totally-missing home only
cross-origin referrer (google.com) Go up, home

npx tsc --noEmit clean; pnpm lint only pre-existing no-img-element warnings.

…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>
@vercel

vercel Bot commented Sep 9, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
sourcegraph-docs Ready Ready Preview Sep 9, 2026 10:26am UTC

Request Review

@marcleblanc2 marcleblanc2 changed the title 404 page: link back to the referring page and up to the nearest existing ancestor 404 page: Link back to the referring page and up to the nearest existing ancestor Sep 9, 2026
@marcleblanc2 marcleblanc2 changed the title 404 page: Link back to the referring page and up to the nearest existing ancestor Link the 404 page back to the referring page and up to the nearest existing ancestor Sep 9, 2026
@marcleblanc2 marcleblanc2 changed the title Link the 404 page back to the referring page and up to the nearest existing ancestor feat/site: Improve 404 page with links back to the referring page and up to the nearest ancestor Sep 9, 2026
…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>
@marcleblanc2 marcleblanc2 changed the title feat/site: Improve 404 page with links back to the referring page and up to the nearest ancestor feat/site: Improve 404 page with links back to the previous page and up to the nearest ancestor Sep 9, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant