From 59019302f4073b354a19924692409b4bd613893e Mon Sep 17 00:00:00 2001 From: Marc LeBlanc <7050295+marcleblanc2@users.noreply.github.com> Date: Thu, 10 Sep 2026 20:24:48 -0600 Subject: [PATCH 1/2] fix/site: Strip basePath from 404 page referrer link document.referrer keeps the /docs basePath in production, and next/link adds it again, so the 404 page's "Go back to" link pointed at /docs/docs/. Strip the basePath and ignore same-origin referrers outside the docs (for example sourcegraph.com/pricing) or the docs root. Vercel previews serve at the root with an empty basePath, which is why the PR preview for #1887 did not show this. Co-authored-by: Amp Amp-Thread-ID: https://ampcode.com/threads/T-01a08e26-0c39-723b-95ad-65455002f541 --- src/components/NotFoundLinks.tsx | 38 ++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/src/components/NotFoundLinks.tsx b/src/components/NotFoundLinks.tsx index fefe9f714..50319e013 100644 --- a/src/components/NotFoundLinks.tsx +++ b/src/components/NotFoundLinks.tsx @@ -22,20 +22,35 @@ function nearestExistingAncestor( return null; } -// The page the user came from on a fresh page load, only when it is on this -// site. document.referrer does not change on client-side navigations, so -// those are covered by usePreviousPathname instead. -function sameOriginReferrer(): URL | null { +// Production serves the docs under /docs (basePath in next.config.js). +const basePath = process.env.NEXT_PUBLIC_DOCS_BASE_PATH || ''; + +interface PageLink { + href: string; + pathname: string; +} + +// The docs page the user came from on a fresh page load. document.referrer +// keeps the basePath, which next/link adds again, so strip it here; pages on +// the same origin outside the docs (sourcegraph.com/pricing) do not count. +// document.referrer does not change on client-side navigations, so those are +// covered by usePreviousPathname instead. +function docsReferrer(): PageLink | null { if (!document.referrer) return null; const referrer = new URL(document.referrer); - return referrer.origin === window.location.origin ? referrer : null; + if (referrer.origin !== window.location.origin) return null; + if (!referrer.pathname.startsWith(`${basePath}/`)) return null; + const pathname = referrer.pathname.slice(basePath.length); + // The home link already covers the root. + if (pathname === '/') return null; + return {href: pathname + referrer.search + referrer.hash, pathname}; } export function NotFoundLinks({pagePaths}: {pagePaths: string[]}) { const pathname = usePathname(); const previousPathname = usePreviousPathname(); const [ancestor, setAncestor] = useState(null); - const [referrer, setReferrer] = useState(null); + const [referrer, setReferrer] = useState(null); const pagePathSet = useMemo(() => new Set(pagePaths), [pagePaths]); // Both values depend on the browser URL, which the statically prerendered @@ -43,7 +58,7 @@ export function NotFoundLinks({pagePaths}: {pagePaths: string[]}) { // mismatch. useEffect(() => { setAncestor(nearestExistingAncestor(pathname, pagePathSet)); - setReferrer(sameOriginReferrer()); + setReferrer(docsReferrer()); }, [pathname, pagePathSet]); // The previous pathname may itself have been a 404. @@ -54,14 +69,9 @@ export function NotFoundLinks({pagePaths}: {pagePaths: string[]}) { // Prefer the in-app history over document.referrer, which goes stale on // client-side navigations. - const backLink = previousPage + const backLink: PageLink | null = previousPage ? {href: previousPage, pathname: previousPage} - : referrer - ? { - href: referrer.pathname + referrer.search + referrer.hash, - pathname: referrer.pathname - } - : null; + : referrer; // Skip the up link when it would repeat the back link. const upLink = From 08cb42f0f06675dfba8bfe6c87e50d39977107e2 Mon Sep 17 00:00:00 2001 From: Marc <7050295+marcleblanc2@users.noreply.github.com> Date: Thu, 10 Sep 2026 20:27:25 -0600 Subject: [PATCH 2/2] Update src/components/NotFoundLinks.tsx Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/components/NotFoundLinks.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/NotFoundLinks.tsx b/src/components/NotFoundLinks.tsx index 50319e013..9d532cb7b 100644 --- a/src/components/NotFoundLinks.tsx +++ b/src/components/NotFoundLinks.tsx @@ -33,7 +33,7 @@ interface PageLink { // The docs page the user came from on a fresh page load. document.referrer // keeps the basePath, which next/link adds again, so strip it here; pages on // the same origin outside the docs (sourcegraph.com/pricing) do not count. -// document.referrer does not change on client-side navigations, so those are +// document.referrer does not change on client-side navigation, so those are // covered by usePreviousPathname instead. function docsReferrer(): PageLink | null { if (!document.referrer) return null;