5.8.9
Performance
How big the search is, what loads when, how to cache its files, and how it works with a strict Content Security Policy.
The search loads in two steps. A small boot script runs on every page. The rest (the engine, the dialog and the index) loads only when a visitor first reaches for search: the first open, or a hover or focus on a search button. A visitor who never searches downloads only the boot.
Script sizes
The package’s tests fail when a script grows past these sizes, so they are upper limits, not hopes.
| Script | When it loads | Gzip, at most |
|---|---|---|
| Astro: boot | Every page | 4.07 KB |
| Astro: app (engine, dialog) | First use | 25.55 KB |
| Embed and EmDash: boot | Every page | 6.29 KB |
| Embed and EmDash: app (dialog) | First use | 26.58 KB |
| Embed and EmDash: results page | On the results page | 24.62 KB |
| Embed and EmDash: first open, boot and app together | First use | 32.87 KB |
The embed’s boot is larger than the Astro one: it reads its settings at run time and takes over the theme’s search. MiniSearch, the engine, is about 6 KB of the app.
The EmDash admin page’s own code is about 5 KB gzip, and it loads only in the admin.
Index size
Every record loads into the visitor’s browser, so the index size is what grows with your site.
Measured on astronomic.dev, before the index stored its records packed (the packed file is about 17% smaller gzipped). The CPU columns are Chromium slowed down 4x, like a mid-range phone.
| Records | Index (gzip) | Load at 4x CPU | Query p95 at 4x CPU (engine only) |
|---|---|---|---|
| 528 | 79 KB | 88 ms | 1.8 ms |
| 1,200 | 121 KB | 136 ms | 2.6 ms |
A record is a page or one of its heading sections. A full dialog query (the engine, then grouping, filter counts and highlights) took about 10 ms at p95 at 4x CPU on the 528 records.
On EmDash, a 10,000-entry site stores its index in 4 chunks, 376 KB gzip, and builds in about 2 seconds.
Limits and warnings
- Budget. Each index has a size budget, 300 KB gzip by default (
budget: { gzipKB }). Going over it is a warning, not an error. On EmDash the admin page shows the size green up to 80% of the budget, amber up to 100% and red above. - EmDash cap. Each locale’s index is capped at 8,000,000 bytes of JSON, the most EmDash lets a plugin route send. Over the cap, the build fails and the old index stays.
- Very large sites. At about 5,000 records, the load on a slowed-down phone passes half a second. The EmDash admin page shows a notice at 5,000 pages. A Pagefind-based engine, which loads only the parts of the index a query needs, is planned for sites that big, but it is not built yet.
Make the index smaller
- Set
clipto keep only the first part of each section’s text (at least 200 characters). Words past the cut are still found, and the result still links to their section. Only the preview shows the start of the section. - Index fewer collections or pages. On Astro, narrow the
includeofhtml(), or usefilteroncollections(). On EmDash, setcollectionsin the plugin options. - Split a large part of the site, such as a component catalog, into its own named index, so the main search does not load it. See Settings.
Caching
Every file the Astro integration writes to /_search/ has a hash in its name, except manifest.json, which names the current index files. Cache the hashed files for a year, and the manifest only briefly, so visitors see a new deploy.
The browser asks for the manifest with cache: "no-cache", so it revalidates each time. If an index file the manifest names is gone (a 404 after a deploy), it fetches the manifest once more and loads the new file.
On Cloudflare (Pages, or Workers static assets), add a public/_headers file:
/_search/*
Cache-Control: public, max-age=31536000, immutable
/_search/manifest.json
! Cache-Control
Cache-Control: public, max-age=0, must-revalidate
On EmDash
- The manifest and index routes (
/_emdash/api/plugins/astronomic-search/manifestand.../index) sendpublic, max-age=0, must-revalidatewith an ETag. A browser revalidates each time, so a new build shows at once. - With no build yet, the manifest answers 503 and the theme’s own search keeps working.
- On the Cloudflare adapter,
searchFiles()writes cache rules for/_search/embed/to_headersfor you: the hashed chunks for a year, and the boot,engine.jsonandfiles.jsonfor 5 minutes. It keeps every other rule in the file.
For the embed
Cache the hashed chunks for a year, and the boot, engine.json and files.json for a few minutes. Change the boot’s ?ver= on each release. Keep the previous release’s files for one more release. See Embed on any server.
Content Security Policy
The search needs no inline script and no inline style:
- The boot and app scripts are files, never inline.
- The dialog’s styles are a constructable stylesheet in its shadow root.
- The embed’s config tag is
<script type="application/json">. The browser never runs it, soscript-srcdoes not apply to it.
A policy of script-src 'self'; style-src 'self' works with no changes. The package’s own browser tests run under exactly that policy.
One thing to watch on Astro: the SearchPage script is a small module that Vite may inline in the page when it is under vite.build.assetsInlineLimit. A strict policy needs it as a file, so do not raise that limit very high.
The search loads nothing from us. Your site serves every file itself, so the policy needs no outside host.