5.8.8
Embed on any server
Add the search to a page that is not built with Astro, such as a CMS theme, with one script and one JSON tag.
The embed is a second build of the search for pages that Astro does not build. It reads its settings at run time from one JSON tag that your server prints, so the site that uses it needs no build step. It uses the same index files, manifest, dialog and --search-* custom properties as the Astro integration.
You do not need this page for EmDash: the EmDash plugin prints the embed on every public page for you. See Install on EmDash.
Never load the embed on an Astro page that uses the integration or the SearchPage component. Each has its own boot script, and two would both answer the shortcut keys.
Build output
pnpm --dir packages/search build:embed
pnpm build writes the same folder, and the npm package ships it as dist/embed/. Copy the whole folder to your server. It holds:
-
search-embed.js: the boot. It has a fixed name and no imports. Load it as a plain module, and change?ver=on each release:<script type="module" src="/search/search-embed.js?ver=1"></script> -
chunks/embed-app-<hash>.jsandchunks/embed-results-<hash>.js: the dialog and the results page. They load only when needed, from paths relative to the boot. Each chunk holds all its own code. Cache them for a year. -
engine.json: the engine the chunks carry (schema,engine,engineVersion). Check that your index files were built for it. -
files.json:{ "boot": "search-embed.js", "files": [...] }, every file written.
When you deploy a new build, keep the files the previous files.json lists for one more release. A cached page or an open tab may still ask for the old chunk names.
The build writes to a staging folder and swaps it in only when it is complete. A failed build leaves the old output as it was.
The config tag
Print one tag per page, before or after the boot:
<script type="application/json" data-search-config>
{
"manifest": "/search-data/manifest.json",
"index": "default",
"results": "/search",
"queryParam": "q",
"paramPrefix": "",
"keys": ["mod+k", "/"],
"strings": { "placeholder": "Search the site" },
"browse": [{ "label": "All guides", "href": "/guides" }],
"takeover": { "enabled": true, "inputName": "q", "skip": ".no-search-takeover", "allowFields": ["lang"] }
}
</script>
Escape <, > and & in the JSON, so the text cannot close the tag. Only manifest is required. Every other key has a default, and a wrong value falls back to it.
| Key | Default | What it does |
|---|---|---|
manifest |
(required) | The manifest URL: an absolute http(s) URL or a root-relative path. With no usable manifest, or a tag that is not JSON, the embed does nothing. |
index |
"default" |
The index for the shortcut keys, takeover, the results page, and placements with no data-search-index. |
lang |
<html lang> |
Picks the index locale. |
results |
"/search" |
The results page URL, for “See all results” and the takeover check. Relative or http(s). |
queryParam |
"q" |
The query parameter on the results URL. |
paramPrefix |
"" |
Goes before the type, index and facet parameters. Letters, digits, _ and - only. |
keys |
[] |
Shortcut keys, such as ["mod+k", "/"]. They open the default index’s dialog. |
strings |
{} |
Dialog and results page text. See Customize. |
browse |
[] |
Links shown when the index cannot load, under the retry button. A link whose href is not relative or http(s) is dropped. With a working index, an empty dialog shows the index’s suggested links and recent searches instead. Those links come from the index’s suggested setting, where the index is built. See Settings. |
takeover.enabled |
false |
Take over the theme’s own search form. |
takeover.inputName |
"q" |
The name of the theme form’s search input. Letters, digits, _, - and [] only. |
takeover.skip |
".no-search-takeover" |
Forms that match this selector, or sit inside it, are left alone. |
takeover.allowFields |
["lang"] |
Hidden input names a form may carry and still be taken over. |
standDown |
null |
The name of another live search on the page. When set, takeover is off. Placements still work. |
theme |
none | "light" or "dark". The dialogs and the takeover’s stand-in box use it whatever the device’s color scheme. Left out, they follow the device. The results page has no colors of its own, so it always follows the page. |
links |
none | "new-tab": every link in the dialog opens in a new tab, and the dialog stays open. Left out (or any other value), a result opens in the same tab. See Links in a new tab. |
The embed does nothing in a browser without constructable stylesheets (Safari before 16.4). Every form and link stays native there.
Links in a new tab
For a page people should not leave, such as an admin screen with a “try a search” box, set "links": "new-tab". Leave it out on pages for visitors. Results, suggested links, “See all results” and the error state’s links then open in a new tab, and the dialog stays open with its query. The dialog’s footer says so (the newTab string). Theme takeover’s form submit and the results page are not affected. Keep the config tag on the page until search first loads: the key is read then, and with no tag left the links open in the same tab.
Placements
A placement is a container that your server prints and styles. The embed draws no field of its own. It adds behavior only, and both kinds work with JavaScript off.
<!-- Inline: a real search field. Typing opens the dialog with the text. -->
<div data-search-placement data-search-open="inline" data-search-index="main" data-search-placeholder="Search products">
<form role="search" method="get" action="/search">
<label for="product-q">Search</label>
<input id="product-q" type="search" name="q">
<button>Search</button>
</form>
</div>
<!-- Modal: a plain link. A click or tap opens the dialog. -->
<div data-search-placement data-search-open="modal">
<a href="/search">Search</a>
</div>
- The inline field’s
namemust equaltakeover.inputName. An inline placement has no results list of its own. - Do not put
data-search-triggeron the modal link. data-search-indexanddata-search-placeholderare optional, and are read at each open. Two placements on one index each show their own placeholder.- A click with Cmd, Ctrl, Shift or Alt held, or with the middle button, opens the results page as usual.
- Escape returns focus to the field or link that opened the dialog.
- Placements added after the page loads work too.
Use a placement when your theme has its own search button or popup that takeover cannot see. See Works with your theme.
Takeover
With takeover.enabled, the theme’s own search form opens the dialog. A form is taken over only when all of these hold:
- It is a GET form to the
resultsURL (same origin and path), with no target other than_self. - It has exactly one control named
inputName, a text or search input. - Its only other named controls are hidden inputs in
allowFields, and submit buttons with noformaction,formmethodorformtarget. - It is not inside
skipand not inside a placement.
Any other form is left alone, so product filters, POST forms and forms that go elsewhere keep working.
If the search cannot load, the theme keeps its search. On submit, when the app or the index fails to load, or is not ready within 4 seconds, the form submits natively with the text typed so far. If the index fails after the theme’s field opened the dialog, the dialog closes, the text goes back into the field, and the field stays the theme’s for the rest of the page.
With takeover on, EmDash’s LiveSearch box opens the dialog too, and the shortcut keys win over the theme’s own. See Works with your theme for the details and a one-minute check.
First tap on phones
iOS opens the keyboard only for a field focused inside the tap itself. So every touch open shows a small stand-in field that looks like the dialog’s search box, and focuses it at once. When the dialog is ready, and at least 150 ms later, the text and the caret move to the real input. Style the stand-in with the same --search-* custom properties as the dialog.
The results mount
On your results page, print a mount with your own plain result list inside it:
<div data-search-results data-search-index="main" data-search-query="lighthouse">
<ol>
<li><a href="/guides/lighthouse">Lighthouse guide</a></li>
</ol>
</div>
- On a page with a mount, the boot loads the results chunk at once. When the index is loaded and the first render is built, the page draws in a shadow root and your list stops showing. If anything fails, your list stays.
data-search-queryis the server’s query, for pretty URLs such as/search/lighthouse/. It applies only to the page’s first history entry.data-search-indexis optional. The URL’s index parameter comes first, then this attribute, then the config’sindex.data-search-typesis optional: a comma-separated list of the types the page shows.- The results page draws its own search field. Replace the body of the theme’s search page with the mount. If you keep the theme’s own field outside the mount, the page shows two search fields. Put the theme’s form and list inside the mount instead: they show when the script does not run.
For colors, parts and the EmDash <SearchResults> component, see The search results page.
The same page is a module too: mountResultsPage(root, config) from @astronomic-dev/search/results returns an unmount function. After an unmount, your list shows again.
URL names
The results URL uses queryParam for the query, ${paramPrefix}type for the type tab, ${paramPrefix}index for the index, and ${paramPrefix}<facet> for each facet pick. “See all results” from a dialog always writes the index, so the results page searches the same one.
These three names are reserved. A facet whose URL name equals the query, type or index name is not read from or written to the URL. A queryParam equal to the type or index name falls back to "q". Set paramPrefix when your server already uses short names, such as tag or paged, for its own parameters.
Caching and security
Cache the hashed chunks for a year, and the boot, engine.json and files.json for a few minutes. The embed loads no inline script, so a strict Content Security Policy works. See Performance.