Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Fast Search for a Static Astro Blog

· 4 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Install Pagefind
    In your project root, run npm install pagefind. This adds the Pagefind CLI and its indexing library.
  2. Build your site
    Run npm run build (or astro build) to generate the static output in dist/. This step is necessary before indexing.
  3. Index your site
    Run npx pagefind --site dist. This scans your built HTML, extracts text content, and creates a search index inside dist/pagefind/. You should see a success message with the number of pages indexed.
  4. Create a Search component
    In src/components/, create Search.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>
  1. Load Pagefind in the component
    Add a <script> tag in the component that dynamically imports Pagefind from the public URL. Use import('/pagefind/pagefind.js') to avoid bundling issues. Then set up a search function that triggers on input events.
  2. 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. Use search("query") to get results, then loop through them with result.data() to extract title and URL.
  3. Style the results
    Add minimal CSS to make the results list readable. You can use your existing design tokens or a simple card layout.
  4. Add the component to your layout
    Import Search into your main layout (e.g., BaseLayout.astro) and place it in the header or a prominent position.
  5. Build and test
    Run npm run build again to include the component, then serve the dist/ folder locally (e.g., npx serve dist). Open the site and test the search box. Type a query and verify results appear.
  6. 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 the base option in astro.config.mjs and adjust the Pagefind script path accordingly.

Tips

  • Use Pagefind’s UI – Instead of building your own interface, you can use the pre-built pagefind-ui package. 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-ignore to 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 in dist/. 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.