Static Site Search with Pagefind
Add lightning-fast, zero-JavaJavaScript search to your static Astro site using Pagefind. This guide covers installation, configuration, and customization.
Search is one of the most requested features for static websites, but implementing it well has traditionally been difficult. External search services like Algolia are powerful but expensive. Client-side solutions like Lunr.js work but require shipping a large JavaScript bundle. Pagefind offers the best of both worlds: a fully static, zero-config search solution that indexes your site at build time and serves results with virtually no runtime JavaScript cost.
What is Pagefind?
Pagefind is a fully static search library that aims to perform well on large sites, while using as little of your users' bandwidth as possible. It runs after your site build, indexes your HTML files, and generates a compact search index that is split into chunks. When a user searches, Pagefind only downloads the specific chunks needed to answer their query, resulting in lightning-fast results even on sites with thousands of pages. The entire search experience runs client-side with no server required.
How Pagefind Works
The magic of Pagefind lies in its chunked index architecture. After your static site is built, Pagefind scans your HTML output files, extracts text content from elements marked with data-pagefind-body, and builds a search index. This index is then split into small, discrete chunks stored as static files. When a user types a search query, Pagefind loads only the relevant chunks over the network — typically just a few kilobytes — and performs the search entirely in the browser. This means search results appear in milliseconds, even on slow connections.
Installing Pagefind in Astro
Integration with Astro is straightforward. First, install the Pagefind package as a development dependency. Then, modify your build script to run Pagefind after the Astro build completes. Pagefind will scan the generated dist/ directory and create its index files inside dist/pagefind/. These files are then served as static assets alongside your site.
Marking Content for Indexing
By default, Pagefind indexes the text content of your pages. However, for best results, you should explicitly mark the main content area using the data-pagefind-body attribute. This prevents navigation menus, footers, and sidebars from polluting your search results. You can also use data-pagefind-meta to attach structured metadata like titles, dates, and descriptions to each indexed page, which Pagefind uses to display richer search results.
Excluding Content
If you want to exclude certain sections from the search index, add the data-pagefind-ignore attribute to any HTML element. This is useful for removing cookie banners, promotional content, or other non-essential text from search results. You can also exclude entire pages by adding a meta tag with data-pagefind-ignore="all" to the page head.
Building the Search UI
Pagefind provides a lightweight JavaScript API that you load from the generated index. In your Astro project, create a dedicated search page component that dynamically imports the Pagefind module. The API provides a simple search() method that returns results with titles, URLs, and contextual excerpts with highlighted matching terms. You can style these results however you like, giving you full control over the search experience.
Customizing the Search Experience
Pagefind offers several configuration options to tailor the search behavior. You can adjust the excerpt length, set the ranking algorithm weight for different fields, filter results by metadata tags, and even create multiple separate indexes for different sections of your site. The pagefind.options() method lets you configure these settings at runtime before performing searches.
Performance Characteristics
One of Pagefind's most impressive features is its performance profile. The entire search library weighs approximately 15KB gzipped. Search queries typically complete in under 5 milliseconds. The chunked index means that even a site with 10,000 pages only loads a few kilobytes of index data per query. This is dramatically different from solutions that require loading the entire search index upfront, which can be megabytes for large sites.
Tip: Always test your search integration by running a full build and preview. The Pagefind index is only generated during the build step, so search will not work during development unless you build first.
Deploying with Pagefind
Since Pagefind generates its index as static files inside your build output, deployment is seamless. Whether you deploy to Netlify, Vercel, Cloudflare Pages, or a simple static file server, the Pagefind assets are included in your deployment automatically. No special server configuration or environment variables are needed. Just build, deploy, and search works.