5.8.1
Install on Astro
Add Astronomic Search to a plain Astro site, pick where its records come from, and add the search page.
On a plain Astro site, search is an Astro integration. It builds the index when you run astro build, and the search button loads it the first time a visitor reaches for search. For an EmDash site, see Install on EmDash instead.
What you need
- Astro 7.
- An Astro site with a header, or some other place for the search button.
Install the package
pnpm add @astronomic-dev/search
npm and yarn work too. The package has no install scripts, so pnpm asks for no pnpm approve-builds step.
Add the integration
Add search() to astro.config.mjs. Give it at least one source:
// astro.config.mjs
import { defineConfig } from "astro/config";
import search, { collections, html } from "@astronomic-dev/search/astro";
export default defineConfig({
site: "https://example.com",
integrations: [
search({
sources: [collections({ docs: { label: "Docs" } }), html({ include: ["/", "/pricing"] })],
browse: [{ label: "Docs", href: "/docs" }],
}),
],
});
browse is a short list of links. Visitors see them when JavaScript is off, and when the index cannot load. Each href must be a page on your site, or the build fails.
The build writes the index, its manifest and two scripts to /_search/. Every setting is in Settings.
Add the search button
Put SearchLink where your header has room, and SearchNoscript beside it:
---
import SearchLink from "@astronomic-dev/search/astro/SearchLink.astro";
import SearchNoscript from "@astronomic-dev/search/astro/SearchNoscript.astro";
---
<header>
<SearchLink class="search-button">Search</SearchLink>
<SearchNoscript />
</header>
The button is a real link to /search. With JavaScript on, it opens the search dialog, and the shortcut keys (Cmd+K or Ctrl+K, and /) open it too. SearchNoscript shows your browse links when JavaScript is off.
SearchLink takes these props:
| Prop | What it does |
|---|---|
index |
The index this button searches. Default: the site’s default index. |
types |
The types its dialog shows, as a list or a comma-separated string. Default: all. |
group |
How the dialog groups results: "type", a facet name, or false. Default: the index setting. |
href |
Where the link goes with no JavaScript, and where “See all results” goes. Default: /search. |
main |
Marks the page’s main button, when it has more than one: the shortcut opens its dialog. Default: the first. |
class |
Your class names. |
The dialog is light or dark as the visitor’s device is. To keep it one of the two, or to follow your site’s own toggle, see Light, dark and toggles.
Add the search page
The button links to /search, and “See all results” in the dialog goes there with ?q=. Add src/pages/search.astro with the SearchPage component inside your layout. See The results page.
Pick your sources
A source is where the records come from. Each page becomes a record, and each heading becomes a section record that links straight to that heading. You can use more than one source.
Content collections
collections() reads your Astro content collections:
collections({
docs: { label: "Docs" },
blog: { label: "Blog", url: "/posts/{slug}" },
});
Each key is a collection name. A collection’s options:
| Option | What it does |
|---|---|
label |
The type’s name in the dialog. |
type |
The records’ type, so two collections can share one tab: hubs: { type: "learn" }. Default: the collection’s name. |
url |
The page’s URL pattern. {id} is the entry’s id, {slug} is its slug field or else its id. Default: /<collection>/{id}. Your base and trailingSlash are added for you. |
filter |
(entry) => boolean: keeps the entries it returns true for. It sees { id, collection, data }, with dates as ISO strings. |
facets |
Data fields to filter by, such as ["theme"]. |
fields |
Extra data fields to index, for collections with no body. |
keywords |
The field that holds extra search words (see below), or false for none. Default: searchKeywords. |
title |
The field that holds the title. Default: title. |
depths |
The heading levels that start a section. Default: [2, 3]. |
Drafts and entries with noindex are always left out. Set url to match your route. After the build, each record’s URL must be a built page and each heading’s anchor an id on that page, or the build fails with a list. A wrong url pattern can never make a dead result.
Built HTML
html() reads the pages that the build wrote:
html({ include: ["/", "/pricing", "/guides/*"], exclude: ["/guides/old"] });
It reads [data-search-body], else main, else the body, and splits the page at its headings. A * matches any one path segment. It also takes type (default pages), label and selector (a CSS selector for the part of the page to read). With no include, it reads every page. /404 and /500 are always left out.
It skips dialogs, anything hidden or aria-hidden="true", and anything inside [data-search-ignore]. Pages with noindex are left out. When a collection and html() give the same URL, the collection’s record wins.
Your own records
records() takes records that your own code returns: a CMS that the package does not know, a component catalog, or actions with no URL.
import search, { records } from "@astronomic-dev/search/astro";
search({
sources: [
records(() => [{ id: "en:/ui/button", kind: "page", url: "/ui/button", type: "kit", title: "Button", text: "", keywords: ["cta"] }], {
types: { kit: { label: "Kit parts" } },
}),
],
});
The build does not check these URLs. They are yours.
Add keywords
A page can carry words that people search for but the page does not use. Then “start” finds a page titled “Lodestar”.
In a content collection, add searchKeywords to the frontmatter:
---
title: Lodestar
searchKeywords: [start, guide, north]
---
A comma list works too: searchKeywords: "start, guide, north". To read another field, set keywords: "tags" on the collection.
For a page that html() reads, add a meta tag:
<meta name="search:keywords" content="start, guide, north">
Only pages take keywords, not headings. A page keeps its first 20, and the build warns about any page with more.
Try it
pnpm build
pnpm preview
Open the site and press Cmd+K or Ctrl+K. Type a word with a typo in it: the right page still shows.
Search also works in astro dev, with one gap. The dev server has no built pages, so html() sources give no records there. Run a build to see them.
Next: The results page.