5.8.2
Install on EmDash
Add the free search plugin to an EmDash site, check the layout, use the admin page, and run it on Cloudflare.
On an EmDash site, search is a plugin. It builds the index from your EmDash content, keeps it fresh when you publish, and prints the search on every public page. It adds one admin page, Search, with the index status, a Rebuild now button and the settings.
It is tested up to EmDash 1.6, including EmDash’s default blog template. It is a native plugin, installed from npm. It is not in EmDash’s plugin registry, which lists sandboxed plugins only.
To see it first, the EmDash search page has a live demo: search a sample site, and change the dialog’s mode, color, corners and font.
For a plain Astro site, see Install on Astro instead.
Install the package
pnpm add @astronomic-dev/search
Add the plugin
Add search() to EmDash’s plugins, and searchFiles() after emdash():
// astro.config.mjs
import { defineConfig } from "astro/config";
import emdash from "emdash/astro";
import { search } from "@astronomic-dev/search/emdash";
import { searchFiles } from "@astronomic-dev/search/emdash/astro";
export default defineConfig({
integrations: [
emdash({
// your database, storage and other EmDash options
plugins: [search()],
}),
searchFiles(),
],
});
searchFiles() copies the search’s files into your build at /_search/embed/ and serves them in astro dev. Your site serves every file itself. Nothing loads from us.
You need no site URL. The index holds paths only, such as /posts/hello, so search links work on any address the site is served at.
search() takes a few options. Most sites need none. They must be plain JSON, because EmDash passes them to the plugin as JSON:
| Option | Default | What it does |
|---|---|---|
collections |
Every collection with search turned on that is routable and not hidden | The collection slugs to index. |
budget |
{ gzipKB: 300 } |
The size warning for each locale’s index, gzipped. |
clip |
The whole text | Characters of each entry’s text the index keeps. |
exclude |
None | Site paths to leave out of search, such as a page the theme has turned off: ["/speaking", "/projects/*", "/sitemap-*.xml"]. Case makes no difference. A * segment is any one segment; a * inside a segment is any run of characters there. See Leave pages out. |
excludeWhen |
None | Site paths to leave out while a switch in a settings entry is set, such as a part the site owner has turned off in the admin: [{ collection: "details", field: "switch_areas", values: ["off"], paths: ["/areas", "/areas/*"] }]. See Leave pages out. |
listPages |
None | Pages that list whole collections, such as a talks page or a FAQ, so a search for an item finds the page. See List pages. |
taxonomyKeywords |
None | Taxonomies whose term labels are keywords of their entries: ["category", "tag"]. See Categories and tags. |
textFields |
None | More fields to search, per collection: { projects: ["problem", "case_*"], areas: ["intro"] }. See More fields. |
filesPath |
/_search/embed/ |
Where the site serves the search’s files. Must match searchFiles({ path }). |
See Settings for all options.
Check the layout
The plugin prints its two tags only where your layout renders <EmDashHead> and <EmDashBodyEnd page={page} />. Most EmDash themes do, including the default templates. A hand-made layout may not.
To check:
- Open a public page of your site.
- View its source.
- Look for
data-search-confignear the end of<body>.
If it is not there, add <EmDashBodyEnd page={page} /> before </body> in your layout. It needs a real page context:
---
import { EmDashBodyEnd, EmDashHead } from "emdash/ui";
import { createPublicPageContext } from "emdash/page";
const { title } = Astro.props;
const page = createPublicPageContext({ Astro, kind: "custom", pageType: "website", title, pageTitle: title, description: null, siteName: "My site" });
---
<html lang="en">
<head>
<meta charset="utf-8" />
<title>{title}</title>
<EmDashHead page={page} />
</head>
<body>
<slot />
<EmDashBodyEnd page={page} />
</body>
</html>
Leave out the page prop and the site still builds, but the page fails when it is served.
The first build
The first build runs by itself within a minute of install. Then press Cmd+K or Ctrl+K, or /, on any public page, and the search dialog opens.
If no collection has search turned on yet, the build waits and runs once one does.
Last, replace the body of your theme’s search page with <SearchResults>, so “See all results” shows the same results. See The results page.
The admin page
Open Search in the EmDash admin. It has two parts: the index and the settings.
Index
A table shows the site’s index:
| Column | What it shows |
|---|---|
| Name | Site search. |
| Size | The index size, gzipped. Green up to 80% of the budget, amber up to 100%, red above. Hover for the budget. |
| Content | How many pages and sections the index holds. |
| Last built | When the last build finished, or Building while one runs. |
Rebuild now starts a build at once. You rarely need it: the index rebuilds by itself when you publish. If a build is already running, yours starts when it ends.
When you set a site URL in EmDash (siteUrl in emdash({ siteUrl }), the SITE_URL environment variable, Settings > General or the setup wizard), the table ends with a “Built for” line that shows it. With no site URL set, the line is not there, and search works the same.
When a build fails, a banner says why, and the old index stays live. The two common reasons:
- “No collection is set up for search.” Turn on search for a collection in EmDash, then rebuild. This one also takes the old index down, so the theme’s own search works again until the next build.
- “Your Cloudflare plan is too small for a search rebuild.” See Cloudflare.
Past 5,000 pages, the page warns that the site is past the size this search is made for.
Settings
The settings take effect on the next page view. No rebuild is needed. Press Save after a change.
| Setting | Default | What it does |
|---|---|---|
| First shortcut, Second shortcut | mod+k and / |
The keys that open the dialog. Click a field and press the keys. Backspace clears it. mod is Cmd on a Mac and Ctrl elsewhere. |
| Take over the theme’s search form | Off | When on, the theme’s own search form opens the search dialog, and the shortcut keys win over the theme’s own. |
| Search input name | q |
The name of the theme’s search field, for takeover. |
| Search box text | Search the site |
The text in the dialog’s search box. With takeover on, it also shows in the theme’s search box. |
| Theme | Auto | Auto follows the visitor’s light or dark setting. Light and Dark fix it. |
| Results page URL | /search |
Where “See all results” goes. It must match your search form’s action exactly, trailing slash included. |
Two known limits: Space cannot be a shortcut key. On a Mac, Ctrl records as ctrl, not mod.
To see how takeover works with your theme, and how to check it in a minute, see Works with your theme.
Cloudflare
On Cloudflare, the plugin needs the Workers Paid plan. Node hosting has no such limit.
A rebuild runs inside the Worker. The Free plan allows 50 D1 queries and 10 ms of CPU per run. A build makes about 8.5 D1 queries per 100 pages, so the Paid plan’s 1,000 queries per run fit about 11,000 pages. When a build hits a plan limit, the old index stays, and the admin page says so.
The rebuild runs on EmDash’s cron. So the Worker needs EmDash’s Worker entry and a Cron Trigger every minute. Without both, no first build runs and no change is picked up.
-
Add the Worker entry:
// src/worker.ts export { default, PluginBridge } from "@emdash-cms/cloudflare/worker"; -
Point
wrangler.jsoncat it, turn onnodejs_compat, and add the trigger:// wrangler.jsonc { "main": "./src/worker.ts", "compatibility_flags": ["nodejs_compat"], "triggers": { "crons": ["* * * * *"] } } -
Run
npx emdash doctor. It checks the Worker entry.
Astro 7.3 needs @astrojs/cloudflare 14.3.x.
To test the cron locally, build the site, run wrangler dev --test-scheduled, and fire the trigger:
curl "http://localhost:8787/cdn-cgi/handler/scheduled?cron=*+*+*+*+*"
Use the port that wrangler dev prints.
If the Worker fails at start with a require error from postcss, add this small Vite plugin, with nodejs_compat on. EmDash’s HTML sanitizer pulls in postcss, and Workers lack the require it calls.
// astro.config.mjs, in vite.plugins
{
name: "workers-require",
applyToEnvironment: (environment) => environment.config.consumer === "server",
renderChunk(code) {
if (!code.includes("typeof require")) return null;
return { code: `import { createRequire as __cr } from "node:module";\nconst require = __cr("/");\n${code}`, map: null };
},
}
What is indexed
- One record for each published entry in the indexed collections, and one index for each locale. The URL comes from EmDash itself, so URL patterns, locale paths and trailing slashes match your site.
- The text is the first of the excerpt, summary, description and SEO description, then the fields in
textFields, then the plain text of the content. - A
search_keywordsfield adds keywords: a list, or a comma list. EmDash field slugs are lowercase, so name it that way. - Pages in
listPagesalso have the words of the entries they list, andtaxonomyKeywordsadds an entry’s terms as keywords. - Left out: drafts, scheduled entries until they publish, entries with
seo.noIndexon, entries with no public URL, and paths inexclude, or inexcludeWhenwhile its switch is set. - Headings are searched, but a result links to the page, not to a section. EmDash’s renderer prints no heading ids yet.
How fast a change shows
About a minute. Each of these marks the index as stale: publish, unpublish, delete, restore, a save of a published entry, schedule and unschedule.
The build runs once edits stop for 10 seconds, or at most a minute after the first edit. A full rebuild also runs every night, and Rebuild now runs one at once.
A change to the plugin’s options in your code (collections, exclude, excludeWhen, clip, listPages, taxonomyKeywords or textFields) does not mark the index as stale. After you deploy one, click Rebuild now.
A build never replaces the live index until it has finished. Search keeps working while it runs.
Size limits
Each locale’s index can be up to 8,000,000 bytes of JSON. That is the most EmDash lets a plugin route send. Over the cap, the build fails, the old index stays, and the admin page says how far over it is.
To get under it, index fewer collections with collections, or set a smaller clip.
The 300 KB gzip budget is a warning only. For scale: a 10,000-entry site has a 376 KB gzip index and builds in about 2 seconds. The admin page’s own code is about 5 KB gzip and loads only in the admin.
Next: The results page.