Fast Search for a Static Astro Blog
In this article
Adding fast, client-side search to a static Astro blog gives visitors instant results without a server or third-party service. This guide uses Pagefind, a low‑JS search library that indexes your built site and works offline. You’ll have a search box that filters posts as you type, with no backend required.
What You'll Need
- An existing Astro blog (version 4 or later recommended)
- Node.js and npm installed
- Basic familiarity with Astro components and the terminal
Step-by-Step
- Install Pagefind
In your project root, runnpm install pagefind. This adds the Pagefind CLI and its indexing library. - Build your site
Runnpm run build(orastro build) to generate the static output indist/. This step is necessary before indexing. - Index your site
Runnpx pagefind --site dist. This scans your built HTML, extracts text content, and creates a search index insidedist/pagefind/. You should see a success message with the number of pages indexed. - Create a Search component
Insrc/components/, createSearch.astro. Add a simple input field and a container for results. For example:
<input type="search" id="search" placeholder="Search posts…">
<div id="results"></div>
- Load Pagefind in the component
Add a<script>tag in the component that dynamically imports Pagefind from the public URL. Useimport('/pagefind/pagefind.js')to avoid bundling issues. Then set up a search function that triggers on input events. - Write the search logic
In the script, create an async function that initializes Pagefind, waits for the user to stop typing (debounce), runs the search, and displays results. Usesearch("query")to get results, then loop through them withresult.data()to extract title and URL. - Style the results
Add minimal CSS to make the results list readable. You can use your existing design tokens or a simple card layout. - Add the component to your layout
ImportSearchinto your main layout (e.g.,BaseLayout.astro) and place it in the header or a prominent position. - Build and test
Runnpm run buildagain to include the component, then serve thedist/folder locally (e.g.,npx serve dist). Open the site and test the search box. Type a query and verify results appear. - Optimize for production
For faster loads, consider preloading Pagefind’s JS with a<link rel="preload">tag. You can also lazy-load the script only when the user focuses the search input.
Common Mistakes
- Forgetting to rebuild before indexing – Pagefind only sees the built HTML. If you change content, rebuild and re-run the index command.
- Bundling Pagefind with Astro – Importing the script directly in a component can cause build errors. Always use a dynamic import from the public path.
- Not debouncing input – Without a delay, every keystroke triggers an index query, which can cause lag. Use a simple setTimeout debounce.
- Ignoring the base path – If your blog is hosted in a subdirectory (e.g.,
/blog/), you must set thebaseoption inastro.config.mjsand adjust the Pagefind script path accordingly.
Tips
- Use Pagefind’s UI – Instead of building your own interface, you can use the pre-built
pagefind-uipackage. It provides a styled search box and results with minimal setup. - Filter by content type – Pagefind supports data attributes to filter results. Add
data-pagefind-filter="category"to your post elements to let users filter by category. - Exclude pages – Add
data-pagefind-ignoreto any element you don’t want indexed, such as navigation or footer text. - Cache the index – Pagefind generates a static index, so it works with any CDN. Ensure your host serves the
pagefind/folder correctly.
Troubleshooting
- Search returns no results – Check that the
pagefind/folder exists indist/. If not, re-run the indexing command. Also verify that your content is in HTML (not rendered client‑side). - Console errors about missing module – Ensure the script path is correct. If your site is in a subfolder, use
import('/your-base/pagefind/pagefind.js'). - Search works locally but not on the live site – This usually happens when the host strips the
pagefind/folder or when the base path is wrong. Check your deployment logs and confirm the folder is uploaded. - Results appear but are unstyled – If you’re using the UI package, make sure you import its CSS. For custom results, check that your CSS selectors match the generated markup.
Frequently Asked Questions
Does Pagefind work with Astro’s collections?
Yes. Pagefind indexes the final HTML, so any content rendered to static pages—including collections—will be searchable.
Can I use this with a CMS like Contentful?
Absolutely. As long as your Astro build outputs static HTML, Pagefind will index the content regardless of its source.
Will this slow down my site?
No. The search index is a set of static files. The JavaScript is small and only loads when needed. Initial page load is unaffected.
How do I update the index when I add new posts?
Simply rebuild your site and run the Pagefind indexing command again. You can automate this with a predeploy script.
Can I search only blog posts, not other pages?
Yes. Use data-pagefind-filter="type" on your blog post elements, then filter in the search call. Alternatively, add data-pagefind-ignore to non‑blog pages.
Is there a way to highlight search terms in results?
Pagefind provides a highlight() method on result data. Use it in your display logic to wrap matched terms in <mark> tags.