Skip to Content
Documentation search

Documentation search

The header search and Cmd+K / Ctrl+K open a command palette. Type a word or phrase, move through results with ↑ / ↓, and press Enter to open the selected page or heading. Escape, the close button or the backdrop dismisses the dialog. On mobile, the same header button stays visible alongside the navigation menu.

Architecture

components/docs-search/DocsSearch.tsx owns the trigger and dialog. I disable Nextra’s Layout.search explicitly, because the theme would otherwise mount its search in both the desktop header and the mobile sidebar. I supply one palette through Navbar.children, which preserves Nextra’s navigation and theme controls. The trigger becomes enabled when its handlers are mounted.

Search uses the site’s own Pagefind  index. There is no search backend, hosted search subscription or AI service. In a production deployment the only analytics request is one count-only event per opening of the dialog (a bucket of the result count, never the query): see Analytics. pagefind.ts dynamically imports /_pagefind/pagefind.js only after opening search, shares the successful initialization promise and clears failed initialization so retry can recover. Retry refreshes the index module, including after a fragment lookup failure, and changes the module URL to bypass the browser’s cache of failed imports. An unavailable index is an explicit error with local setup guidance; popular sections remain available before a query.

Queries are trimmed and debounced for 160 ms. An effect cleanup prevents old results from replacing a newer query or a closed dialog. Queries longer than 200 characters get guidance rather than starting a lookup. Pagefind’s relevance order is retained; the first ten matching pages contribute at most twenty distinct heading results. A page title is shown as context when a matching heading has its own title. The status announces matching pages separately from the displayed results.

lib/docs-search.mjs normalizes Next’s indexed .html URLs into App Router paths, preserves query strings and anchors, rejects off-site URLs and removes the configured base path before passing a URL to next/link. Excerpts are split into text and bare <mark> boundaries, then rendered as React children. HTML from the index is never injected into the page. Unknown tags and attributes cannot become live elements.

The native modal <dialog> supplies the inert background; explicit Tab wrapping keeps focus on the dialog controls across browsers. The input follows the combobox/listbox pattern with an active descendant; focus stays in the input while arrow keys select a result. The dialog restores focus to the invoking element, announces state through a polite live region, supports keyboard-only dismissal and disables its entrance animation with reduced motion. Result links also remain navigable through assistive technology. No live processes or external providers are needed by the index.

Build and run locally

From docs-site/, using the Node version in the repository’s .nvmrc and the PNPM version pinned in packageManager:

pnpm install --frozen-lockfile pnpm build pnpm start

postbuild (scripts/postbuild.mjs) runs Pagefind on the static export in out/ and writes the index into the deployed site (out/_pagefind) and to public/_pagefind (so pnpm dev can search after one build). Nextra marks document content with data-pagefind-body; Pagefind indexes the actual rendered HTML and heading anchors, excluding surrounding navigation. The generated directories are ignored by Git. The index must stay inside out/, which is the whole deployment. Build output reports the number of indexed pages and words; a successful Next build without a successful postbuild does not validate search.

For iteration, build once to generate the index, then start pnpm dev. Rebuild whenever documentation content changes to refresh search. A fresh development checkout without an index intentionally shows an unavailable-search message after entering a query.

On the separate docs deployment the default index URL is /_pagefind/pagefind.js, independent of the portfolio origin. To deploy under a path, set DOCS_BASE_PATH=/docs at build time, then build and run with the same value. A static export puts its files at the root of out/ whatever the base path is, so postbuild moves them into out/<base path>/ to match the generated URLs, and keeps a copy of 404.html at the root, where Vercel looks for it. The Next config exposes this path to the client and the loader initializes Pagefind with the matching base URL. Next links add the prefix once. The environment variable must be an absolute path without a trailing slash; it is configuration, not a credential.

Tests and maintenance

pnpm test # Once per Playwright version, unless using an existing Chromium: pnpm exec playwright install --with-deps chromium pnpm build pnpm test:search

The browser suite starts the production docs server on port 3100. It exercises the real generated index, heading navigation and reloads. Deterministic index fixtures cover delayed queries, stale responses, missing/recovering indexes, unsafe excerpt input and long titles. Keyboard tests verify selection, focus containment, dismissal and restoration; axe checks the open dialog’s accessibility tree. Light desktop, dark tablet and narrow mobile screenshots cover initial and result states. A separate test exercises 200% text scaling and reduced motion.

To use system Chromium, set DOCS_CHROMIUM_PATH=/usr/bin/chromium. Screenshot rendering depends on the installed browser, OS and fonts: use the same environment when reviewing or updating baselines. After an intentional visual change, run pnpm test:search --update-snapshots, inspect the images, and commit them with the change. Browser artifacts and traces are ignored.

For a base-path regression run, build with DOCS_BASE_PATH=/docs, then run DOCS_BASE_PATH=/docs pnpm test:search. Stop any earlier docs server first so the suite starts against that build. Restore a normal build afterwards when working at /.

Automated ARIA and axe checks do not replace a screen-reader check. Before release, verify that VoiceOver or NVDA announces the dialog name, result counts and active option, that Escape restores focus, and that navigation still reaches the chosen heading. Also review touch interaction and browser zoom on the target browsers. Keep the index local and the theme override coordinated when upgrading Nextra; an AI Q&A interface, if added later, belongs in a separate feature with its own retrieval and citation contract.

Last updated on