Building a static site with an AI assistant is fast, but adding search usually feels like a heavy lift. You don't need a backend, a database, or a monthly subscription to get decent results. Pagefind generates a static index directly from your finished HTML files, allowing you to keep your site on simple hosting while offering a robust search interface. The key is understanding that Pagefind works on the output, not the source templates, which changes how you approach deployment.
Identify the Output Before Indexing
Before you touch any configuration, you must know exactly which folder your host serves. Whether it is dist, public, or a raw HTML directory, Pagefind needs to point at the final build. Open a generated page and verify the text exists in the HTML source. If your site relies on client-side hydration to load content, Pagefind will index empty shells. You need static text in the DOM for the indexer to see it. Start by defining a single reader task, like finding parking instructions, and confirm that phrase exists in the built HTML.
Control the Index Scope
Pagefind indexes everything by default, which is rarely what you want. You can restrict the scope by adding the data-pagefind-body attribute to the main content area of your templates. Once this marker is present on any page, Pagefind excludes all pages that lack it. This is crucial for keeping utility pages, test files, and unpublished drafts out of search results. Remember that excluding a page from search does not hide its URL; anyone with the direct link can still access it. Do not rely on search exclusion for privacy.
Integrate the Component UI
The Pagefind Component UI provides a ready-made search dialog that requires minimal setup. You link the CSS and JS files from the generated /pagefind/ directory and add the and web components to your layout. These assets are created during the indexing step, so they won't exist until you run the build command. If you are deploying to a subdirectory, ensure your asset paths are adjusted accordingly. The search control should be integrated into your existing navigation, preserving usability without replacing useful menu items.
Build, Index, and Verify
The workflow is strict: run your standard website build first, then execute the Pagefind CLI against the output folder. For example, npx -y pagefind@1.6.0 --site dist --serve indexes the dist folder and starts a local preview. After deployment, you must upload the generated search bundle alongside your HTML files. If you only upload the HTML, the search feature will break because the supporting assets are missing. Test with a known phrase, an unmatched query to check for proper no-results messages, and keyboard navigation to ensure accessibility.
Key Takeaways
- Pagefind indexes static HTML output, not source files, so ensure content is rendered in the DOM before indexing.
- Use
data-pagefind-bodyto exclude unwanted pages from the index, but remember this does not hide URLs. - The search assets are generated during the build step and must be deployed with the site to function.
- Testing should include verifying that result links open the correct content and that the interface works on narrow screens.
The Bottom Line
If you are shipping a static site, you should not be paying for search infrastructure. Pagefind is a pragmatic, open-source solution that respects the simplicity of static hosting while delivering a professional user experience. Just make sure your build pipeline includes the indexing step, or you will wonder why your search bar is silent.