Skip to content

fix/site: Link search results to current site instead of assuming prod - #1875

Open
marcleblanc2 wants to merge 1 commit into
mainfrom
search-results-relative-urls
Open

fix/site: Link search results to current site instead of assuming prod#1875
marcleblanc2 wants to merge 1 commit into
mainfrom
search-results-relative-urls

Conversation

@marcleblanc2

@marcleblanc2 marcleblanc2 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Problem

On a Vercel preview deployment (or local dev), selecting a search result always navigates to production (https://sourcegraph.com/docs/...) instead of the matching page on the deployment you're viewing.

Cause: the Algolia crawler indexes prod, so every hit's url is an absolute prod URL. The vendored DocSearch renders it verbatim in Hit.tsx (<a href={hit.url}>) and returns it from getItemUrl (Enter-key navigation), so nothing ever makes it relative to the current origin.

Fix

Pass a transformItems to DocSearch in src/components/search/Search.tsx that rewrites https://sourcegraph.com/docs/<path>${NEXT_PUBLIC_DOCS_BASE_PATH}/<path>. NEXT_PUBLIC_DOCS_BASE_PATH is already /docs in production and '' elsewhere (see next.config.js), so:

  • prod: /docs/batch-changes/view-batch-changes#viewing-batch-changes (same target as today)
  • preview / local: /batch-changes/view-batch-changes#viewing-batch-changes, resolved against the current origin

transformItems runs before saveRecentSearch, so recent/favorite searches persisted in localStorage get the rewritten URL too. URLs that don't start with the prod prefix are left untouched.

Not changed

  • Index content: results still reflect what the crawler last saw on prod. Pages added/renamed only in a PR won't show up in search (or may 404 on the preview). Only the link target is localized.
  • No Algolia/crawler config changes.

Verification

Ran locally (pnpm dev, empty base path, same as preview):

  • Searched "batch changes"; result anchors have href="/batch-changes/..." and resolve to http://localhost:<port>/batch-changes/....
  • Pressing Enter on a result navigated to http://localhost:<port>/batch-changes/view-batch-changes#viewing-batch-changes.
  • localStorage.__DOCSEARCH_RECENT_SEARCHES__sourcegraph stored the relative URL.
  • npx tsc --noEmit passes.

Vercel preview (this PR)

Tested on the preview deployment https://sourcegraph-docs-c9sonegt9-sourcegraph-f8c71130.vercel.app:

  • Searched "batch changes"; result anchors render with relative hrefs that resolve to the preview origin, e.g. /batch-changes/view-batch-changes#viewing-batch-changeshttps://sourcegraph-docs-c9sonegt9-sourcegraph-f8c71130.vercel.app/batch-changes/view-batch-changes#viewing-batch-changes.
  • Pressing Enter on the first result navigated to that preview URL (page title "Viewing Batch Changes - Sourcegraph docs", <h1> "Viewing Batch Changes"), not to sourcegraph.com/docs.
  • Recent-searches localStorage entry stored the relative URL /batch-changes/view-batch-changes#viewing-batch-changes.

Production behavior (/docs/<path> prefix) is unchanged by design but can only be confirmed after merge.

Amp threads

The Algolia crawler indexes production, so every hit URL is absolute
(https://sourcegraph.com/docs/...). DocSearch rendered those verbatim,
so selecting a result on a Vercel preview (or local dev) always jumped
to prod.

Pass a transformItems to DocSearch that strips the prod prefix and
prepends NEXT_PUBLIC_DOCS_BASE_PATH, so hrefs resolve against the
deployment being viewed: /docs/<path> in prod, /<path> on previews.

Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-01a07e6c-cddd-72b1-b3bf-98ad32f9d55a
@vercel

vercel Bot commented Sep 8, 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 8, 2026 1:26am UTC

Request Review

@marcleblanc2 marcleblanc2 changed the title Search: send results to the current deployment instead of prod Search: send results to the current deployment instead of always assuming prod Sep 8, 2026
@marcleblanc2
marcleblanc2 marked this pull request as ready for review September 8, 2026 01:35
@marcleblanc2 marcleblanc2 changed the title Search: send results to the current deployment instead of always assuming prod Search: Send results to the current deployment instead of always assuming prod Sep 9, 2026
@marcleblanc2 marcleblanc2 changed the title Search: Send results to the current deployment instead of always assuming prod fix/site: Send search results to the current deployment instead of always assuming prod Sep 9, 2026
@marcleblanc2 marcleblanc2 changed the title fix/site: Send search results to the current deployment instead of always assuming prod fix/site: Link search results to current site instead of assuming prod Sep 9, 2026
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