{
  "id": "add-pagefind-search-to-astro",
  "type": "playbooks",
  "category": "playbooks",
  "locale": "en",
  "url": "/playbooks/add-pagefind-search-to-astro",
  "title": "Prompt-to-PR: Add Pagefind Search to an Astro Site",
  "description": "The full SOP for adding static, zero-backend search to an Astro site with Pagefind — from first prompt to PR description.",
  "tools": [
    "Cursor",
    "Claude Code"
  ],
  "stack": [
    "Astro",
    "TypeScript"
  ],
  "tags": [
    "search",
    "astro",
    "pagefind"
  ],
  "difficulty": "easy",
  "updated": "2026-06-08",
  "markdown": "A complete playbook: requirement, first prompt, expected changes, review, tests,\nlikely failures, fix prompt, and the PR description.\n\n## 1. Requirement\n\nAdd full-text search to a static Astro site with no server and no database.\n\n## 2. First Prompt\n\n```txt title=\"First Prompt\"\nAdd Pagefind search to this Astro static site.\n\n- Add `pagefind` as a devDependency.\n- Update the build script to run `astro build && pagefind --site dist`.\n- Create a `/search` page that mounts Pagefind UI from `/pagefind/pagefind-ui.js`.\n- Mark the main article body with `data-pagefind-body` and add\n  `data-pagefind-ignore` to nav/footer.\n- Do not introduce a server adapter; this stays fully static.\n```\n\n## 3. Expected File Changes\n\n```txt\npackage.json                 (build script)\nsrc/pages/search.astro       (new)\nsrc/layouts/BaseLayout.astro (data-pagefind-ignore on chrome)\n```\n\n## 4. Review Checklist\n\n- Build script runs Pagefind *after* `astro build`.\n- Search page degrades gracefully in `astro dev` (index only exists post-build).\n- Nav/footer are excluded from the index.\n\n## 5. Test Commands\n\n```bash\nbun run build\nbun run preview\n# open /search and query a known term\n```\n\n## 6. Common Failures\n\n- Mounting Pagefind UI before the script loads → `PagefindUI is not defined`.\n- Running Pagefind before the build, so it indexes nothing.\n\n## 7. Fix Prompt\n\n```txt title=\"Fix Prompt\"\nPagefind indexed an empty site. Ensure the build runs `astro build` first,\nthen `pagefind --site dist`, and that the UI script loads before init.\n```\n\n## 8. PR Description\n\n```md title=\"PR description\"\nAdd static full-text search via Pagefind. Indexes `dist` at build time;\nno backend. New `/search` page; nav/footer excluded from the index.\n```"
}