From 46a88b882314b5545a0397250434ccbfb475e8af Mon Sep 17 00:00:00 2001 From: Marc LeBlanc <7050295+marcleblanc2@users.noreply.github.com> Date: Tue, 8 Sep 2026 01:24:38 +0000 Subject: [PATCH] Search: rewrite Algolia hit URLs to the current deployment 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/ in prod, / on previews. Co-authored-by: Amp Amp-Thread-ID: https://ampcode.com/threads/T-01a07e6c-cddd-72b1-b3bf-98ad32f9d55a --- src/components/search/Search.tsx | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/components/search/Search.tsx b/src/components/search/Search.tsx index d56736d48..545c07a62 100644 --- a/src/components/search/Search.tsx +++ b/src/components/search/Search.tsx @@ -1,10 +1,26 @@ import {useEffect, useState} from 'react'; import {searchMetadata} from '../../data/search'; import {DocSearch} from './docsearch/DocSearch'; +import type {DocSearchHit} from './docsearch/types'; import './docsearch/docsearch.css'; // import '@docsearch/css'; +// The Algolia crawler indexes production, so every hit URL is absolute +// (https://sourcegraph.com/docs/...). Rewrite them to root-relative paths so +// results resolve against whichever deployment is being viewed (Vercel +// preview, local dev, or prod) instead of always jumping to prod. +const PROD_DOCS_URL_PREFIX = 'https://sourcegraph.com/docs'; +const basePath = process.env.NEXT_PUBLIC_DOCS_BASE_PATH || ''; + +const toLocalUrl = (url: string): string => + url.startsWith(PROD_DOCS_URL_PREFIX) + ? basePath + url.slice(PROD_DOCS_URL_PREFIX.length) + : url; + +const transformItems = (items: DocSearchHit[]): DocSearchHit[] => + items.map(item => ({...item, url: toLocalUrl(item.url)})); + const getInitialQuery = () => { if (typeof window !== 'undefined' && window?.location?.href) { const url = new URL(window.location.href); @@ -32,6 +48,7 @@ export const Search = () => { apiKey={algoliaConfig.apiKey} initialQuery={initialQuery} maxResultsPerGroup={algoliaConfig.maxResultsPerGroup} + transformItems={transformItems} /> ); };