Pagefind vs Algolia is a choice between a static search index and a hosted search service. Pagefind indexes your built Astro site after astro build and serves search from static files with no server, account or API key, which makes it the right default for blogs, docs and marketing sites. Algolia is a paid, hosted search API with typo tolerance, analytics, merchandising and instant results at large scale, worth it for big catalogs or when search is a core product feature.
Key Takeaways
- Pagefind runs once after the build and writes a chunked index into
dist/pagefind/; browsers download only the pieces a query needs.- Algolia stores your records on its servers and answers queries over an API; you push records on every content change.
- Pagefind is free and private by design. Algolia has a free tier but charges by usage beyond it.
- Starlight ships with Pagefind built in; Algolia DocSearch is free for qualifying open-source documentation.
- Choose Algolia for e-commerce, very large or frequently changing content, typo tolerance and search analytics.
How Pagefind works
Pagefind reads the HTML files in your build output, extracts text, and writes a compressed, chunked index next to your site. When a visitor searches, the browser loads a small entry file and then fetches only the index fragments that match the query terms. There is nothing to host besides static files, so it works on Cloudflare Pages, Netlify, Vercel, GitHub Pages or an S3 bucket.
Because it indexes final HTML, Pagefind sees exactly what visitors see: content from Content Collections, CMS data rendered at build time, and component output. You control what gets indexed with HTML attributes rather than an indexing script.
How Algolia works
Algolia is a search engine you call over the network. You define records (usually one per page or per section), send them to an Algolia index with an admin API key, and query that index from the browser with a search-only key. Algolia handles ranking, typo tolerance, synonyms, faceting and analytics on its side and returns results in milliseconds from its global infrastructure.
The cost is plumbing. Your build or CMS has to push updated records whenever content changes, and you manage keys, index settings and usage limits.
Pagefind vs Algolia at a glance
| Pagefind | Algolia | |
|---|---|---|
| Type | Static index generated after build | Hosted search API |
| Hosting | Your static host | Algolia’s servers |
| Account or API keys | None | App ID, admin key, search key |
| Cost | Free, open source | Free tier, then usage-based plans |
| Indexing | Automatic from built HTML | You push records with a script or connector |
| Updates | On every build | Any time, via API |
| Typo tolerance | Limited | Strong, configurable |
| Filters and facets | Yes, via data-pagefind-filter |
Yes, advanced faceting |
| Multilingual | Yes, separate index per lang |
Yes, per-index language settings |
| Analytics | None built in | Search analytics, click and conversion tracking |
| Privacy | Queries never leave the visitor’s browser and your host | Queries go to Algolia |
| Works offline or behind auth | Yes | Needs network access to Algolia |
| Best for | Blogs, docs, marketing sites | Large catalogs, e-commerce, search-heavy apps |
Adding Pagefind to Astro
Install Pagefind and run it after the build. The simplest setup needs no Astro integration at all:
npm install -D pagefind
// package.json
{
"scripts": {
"build": "astro build",
"postbuild": "pagefind --site dist"
}
}
If your project uses on-demand rendering with an adapter, static files land in dist/client/ instead, so point Pagefind there with --site dist/client.
Then add the default UI to a search page:
---
// src/pages/search.astro
import Base from '../layouts/Base.astro';
---
<Base title="Search">
<link href="/pagefind/pagefind-ui.css" rel="stylesheet" />
<div id="search"></div>
<script is:inline src="/pagefind/pagefind-ui.js"></script>
<script is:inline>
window.addEventListener('DOMContentLoaded', () => {
new PagefindUI({ element: '#search', showSubResults: true });
});
</script>
</Base>
The scripts use is:inline because the /pagefind/ files only exist after the build, so Astro should not try to bundle them. Search therefore works in astro preview, not in astro dev.
Tell Pagefind what to index by marking your main content and excluding chrome:
---
// src/layouts/Post.astro
const { title, category } = Astro.props;
---
<article data-pagefind-body>
<h1>{title}</h1>
<span data-pagefind-filter="category" hidden>{category}</span>
<nav data-pagefind-ignore>...</nav>
<slot />
</article>
Once any page has data-pagefind-body, Pagefind only indexes pages that include it, which keeps tag archives and pagination out of results. The Pagefind docs list every attribute, including data-pagefind-meta and data-pagefind-sort.
Adding Algolia to Astro
A typical static Astro setup indexes content at build time with a script, then queries from the browser.
npm install algoliasearch
// scripts/index-algolia.mjs
import { algoliasearch } from 'algoliasearch';
import fs from 'node:fs/promises';
import path from 'node:path';
import matter from 'gray-matter';
const client = algoliasearch(process.env.ALGOLIA_APP_ID, process.env.ALGOLIA_ADMIN_KEY);
const dir = 'src/content/blog';
const files = (await fs.readdir(dir)).filter((f) => /\.mdx?$/.test(f));
const objects = await Promise.all(
files.map(async (file) => {
const { data, content } = matter(await fs.readFile(path.join(dir, file), 'utf8'));
const slug = file.replace(/\.mdx?$/, '');
return {
objectID: slug,
title: data.title,
description: data.description,
url: `/blog/${slug}/`,
content: content.slice(0, 8000),
};
})
);
await client.saveObjects({ indexName: 'blog', objects });
console.log(`Indexed ${objects.length} posts`);
Run it after a successful build in CI, never in the browser, because the admin key can write to your index. On the client, use the search-only key:
---
// src/components/AlgoliaSearch.astro
---
<input id="q" type="search" placeholder="Search posts" aria-label="Search posts" />
<ul id="results"></ul>
<script>
import { liteClient } from 'algoliasearch/lite';
const client = liteClient(import.meta.env.PUBLIC_ALGOLIA_APP_ID, import.meta.env.PUBLIC_ALGOLIA_SEARCH_KEY);
const input = document.getElementById('q') as HTMLInputElement;
const list = document.getElementById('results')!;
input.addEventListener('input', async () => {
const { results } = await client.search({ requests: [{ indexName: 'blog', query: input.value }] });
const hits = 'hits' in results[0] ? results[0].hits : [];
list.innerHTML = hits.map((h: any) => `<li><a href="${h.url}">${h.title}</a></li>`).join('');
});
</script>
For production you would escape the hit fields or use Algolia’s InstantSearch widgets, which handle highlighting and escaping for you. Store the keys as environment variables; the Astro environment variables docs explain the PUBLIC_ prefix.
Relevance and search quality
Algolia is the stronger engine. It tolerates typos (“astor” finds “astro”), lets you configure synonyms, pin results, boost by popularity, and tune ranking per index. For shoppers who type fast and expect the right product first, that matters.
Pagefind is good enough for most content sites. It does stemming, so “deploying” matches “deploy”, ranks by term frequency and position, supports filters and sub-results that jump to the matching heading, and handles multilingual sites by building a separate index per page language. It does not try to correct misspelled words in the same way.
Cost, privacy and maintenance
Pagefind has no running cost. The index is a few static files served by the host you already pay for (or use for free). There are no keys to rotate, no usage limits, and no third party seeing your visitors’ queries, which also keeps your privacy policy simple.
Algolia’s free tier covers small sites, and paid plans scale with records and search requests. You also maintain the indexing script, handle failures in CI, and watch usage. In return you get a search analytics dashboard showing what people look for and what returns nothing, which is valuable input for a content plan.
Scale and content freshness
Pagefind’s chunked index scales to large static sites because a browser never downloads the whole index. Indexing time grows with page count, and the index only updates when you rebuild, which is fine for sites that rebuild on publish.
Algolia updates instantly, independent of builds. If prices or stock change hourly, or content comes from a database rather than files, a hosted index is the practical choice. It also suits sites rendered on demand where there is no complete set of HTML files to index.
Choose Pagefind or Algolia
Choose Pagefind if your site is a blog, documentation or marketing site, you deploy static output, you want zero cost and no third-party requests, or you use Starlight, where it is already set up.
Choose Algolia if you run a store or large catalog, content changes between builds, typo tolerance and merchandising matter, you want search analytics, or you maintain open-source docs that qualify for free DocSearch.
Start with Pagefind and switch later if you are unsure. Moving to Algolia only changes the search page and adds an indexing step; your content and pages stay the same.
If you are building a content site, the Astro performance guide explains why static, build-time features like Pagefind keep pages fast, and the Starlight decision guide covers docs sites where search comes built in. Komal themes deploy as static files, so Pagefind drops into any of them with the postbuild script above. See documentation themes for search-heavy layouts, and the static deployment docs for hosting.
FAQ
Is Pagefind free?
Yes. Pagefind is open source and runs as a command after your build. It writes the search index as static files into your output folder, so there is no service to pay for and no API key. Hosting the index costs the same as hosting the rest of your static site.
Does Pagefind work with Astro?
Yes. Run pagefind --site dist after astro build, usually as a postbuild script, then load /pagefind/pagefind-ui.js on a search page. Starlight, Astro’s docs framework, includes Pagefind by default. Search only works after a build, so test it with astro preview.
Is Algolia better than Pagefind?
Algolia has better typo tolerance, ranking controls, analytics and instant index updates, so it is better for e-commerce and large, fast-changing content. Pagefind is free, private and needs no backend, which makes it the better fit for most blogs, docs and marketing sites.
Can I use Algolia on a static Astro site?
Yes. Push your content to an Algolia index with a script that runs in CI after each build, then query it from the browser with a search-only API key. The site stays static; only the search requests go to Algolia.
Does Pagefind support filters and multiple languages?
Yes. Add data-pagefind-filter attributes to expose filters such as category or tag in the UI. For multilingual sites, Pagefind reads each page’s lang attribute and builds a separate index per language, so results match the visitor’s language.