Free Astro tutorials for complete beginners.

Beginner's Guide to Astro

How to Add Search to an Astro Site With Pagefind

Pagefind gives your static Astro site a real search box in about ten minutes, with no server and no monthly fee. It's what AstroBeginner uses.

Tested on Astro 7.4

Cover image for How to Add Search to an Astro Site With Pagefind
In this guide
  1. Step 1: Install Pagefind
  2. Step 2: Run it after every build
  3. Step 3: Make a search page
  4. Step 4: Build and try it
  5. Make it better
  6. Where to go next
  7. Frequently asked questions

Search is the feature people assume a static site can’t have. It can. Pagefind builds the search index at the same time your site builds, as plain files. The browser does the rest. It’s what powers the search box on this site, and it took about ten minutes.

Step 1: Install Pagefind

In your project folder:

npm install -D pagefind

Step 2: Run it after every build

Open package.json and find the "build" line under "scripts". Change it to run Pagefind right after Astro:

"build": "astro build && pagefind --site dist"

--site dist tells Pagefind where your built site is. Pagefind reads every page in that folder and writes its index into dist/pagefind/.

Step 3: Make a search page

Create src/pages/search.astro:

---
import Layout from '../layouts/Layout.astro';
---
<Layout>
  <main>
    <h1>Search</h1>
    <link href="/pagefind/pagefind-ui.css" rel="stylesheet" />
    <script is:inline src="/pagefind/pagefind-ui.js"></script>
    <div id="search"></div>
    <script is:inline>
      window.addEventListener('DOMContentLoaded', () => {
        new PagefindUI({ element: '#search', showSubResults: true });
      });
    </script>
  </main>
</Layout>

The is:inline parts matter. Those two files don’t exist until after the build, so Astro must not try to process them. is:inline tells it to leave them alone.

Step 4: Build and try it

npm run build
npm run preview
Terminal
$ npm run build
Running Pagefind v1.6.0 (Extended)
Indexed 1 language
Indexed 5 pages
Indexed 48 words
Finished in 0.027 seconds

Open localhost:4321/search and type a word from one of your pages. Results appear as you type, with the matching text highlighted.

We ran this on a fresh Astro 7.4 project. Five pages indexed, search working, no errors.

Make it better

  • Index only the content. Put data-pagefind-body on your <main> or article element. This is the single biggest quality fix.
  • Hide pages from search. Add data-pagefind-ignore to anything you don’t want indexed, like a thank-you page.
  • Add it to your header. Link to /search/ from your menu, or open it in a dialog like we do. The Pagefind docs show how to call it from your own JavaScript.

Where to go next

Frequently asked questions

Does search need a server?

Not with Pagefind. It builds a search index as files when you build your site. The visitor's browser does the searching. Nothing runs on a server.

Does it work in development?

Only after a build. Pagefind indexes the built site. Run npm run build, then npm run preview, to try it. That's normal.

How big is it?

Small. It loads only the pieces of the index it needs for each search. The search box on this site, with 180 pages, loads well under 100 KB.

Stuck on a step?

Check the common errors guide, look up a word in the glossary, or ask the friendly people in the official Astro and EmDash Discords. How to ask so people can help.

Keep reading

  1. How to Register a Domain Name and Connect It to Your Astro Site

    Buy a domain for about $11 a year, then point it at Cloudflare or Netlify. We did this for astrobeginner.com, and these are the exact steps.

    Tutorials, 8 minute read, November 8, 2026

  2. How to Add Google Analytics to an Astro Site (and a Simpler Option)

    Paste Google's tag into your Astro layout with one small change, is:inline, and it works on every page. Or use Cloudflare Web Analytics, which needs no cookie banner.

    Tutorials, 6 minute read, November 8, 2026

  3. How to Add a Contact Form to an Astro Site (No Server Needed)

    A static site can't receive form submissions on its own. A free form service can. Here's a working contact form in ten lines, with spam protection built in.

    Tutorials, 7 minute read, November 8, 2026

Try "blog", "deploy" or "EmDash".