5.8.4
The results page
Add the search results page that "See all results" and the no-JavaScript link go to, on Astro, on EmDash or on any server.
The dialog shows the best matches. The results page shows them all. Visitors land there from “See all results” in the dialog, which adds the query as ?q=. They also land there when JavaScript is off, because the search button is a real link to it.
Each setup has its own way to add it:
- Plain Astro: the
SearchPagecomponent. - EmDash: the
SearchResultscomponent, around the theme’s own search page. - Any other server: a results mount, a plain
div.
On Astro
Add src/pages/search.astro with SearchPage inside your own layout. Keep the noindex tag, so search engines skip the page:
---
// src/pages/search.astro
import SearchPage from "@astronomic-dev/search/astro/SearchPage.astro";
---
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="robots" content="noindex" />
<title>Search</title>
</head>
<body>
<main>
<SearchPage />
</main>
</body>
</html>
Use your own layout in place of this html shell. SearchPage reads ?q=, lists the matching pages, and shows your browse links with JavaScript off. It reads <html lang> to pick the index’s language.
It takes these props:
| Prop | Default | What it does |
|---|---|---|
heading |
Search |
The page heading. Pass "" to leave it out when your layout has its own. |
label |
Search this site |
The search box’s label. |
button |
Search |
The search button’s text. |
class |
none | Your class names. |
The search button links to /search, with your base, and with a trailing slash when your trailingSlash setting calls for one. If your search page lives somewhere else, set href on each SearchLink.
SearchPage is a plain list. Some limits:
- It searches the default index. It ignores the type and filter parameters that a button may add.
- It shows one list by score, with no groups, so the index’s
group.pagesetting has no effect here. - It lists the first 50 matches. The status line still counts them all.
- It fires no analytics events. Only the dialog does on plain Astro.
For another index or other markup, write the page yourself. Start from dist/astro/SearchPage.astro in the package, and change its relative imports to the package’s own:
---
import SearchNoscript from "@astronomic-dev/search/astro/SearchNoscript.astro";
---
<script>
import assets from "virtual:astronomic-search/assets";
import { loadSearcher } from "@astronomic-dev/search/controller";
import { createSearch, minisearchEngine } from "@astronomic-dev/search/core";
// the rest of the script as it is in the package
</script>
Put the other index’s name in place of assets.index.
On EmDash
Most EmDash themes have a search page already. Keep it, and wrap its body in <SearchResults>:
- Open the theme’s search page, usually
src/pages/search.astro. - Import
SearchResults. - Put the theme’s own search form and result list inside
<SearchResults query={query}>, inside the theme’s container.
---
import { getContentSeo, getEmDashEntry, search } from "emdash";
import SearchResults from "@astronomic-dev/search/emdash/astro/SearchResults.astro";
import Base from "../layouts/Base.astro";
// Your theme's own link helper: the one its list pages use for each entry's URL.
import { entryUrl } from "../utils/urls";
export const prerender = false;
const query = Astro.url.searchParams.get("q")?.trim() || "";
const found = query ? (await search(query, { limit: 30 })).items : [];
// EmDash's search skips drafts only. Drop entries with "noindex" on, as the search index does.
const shown = await Promise.all(
found.map(async (item) => {
const { entry } = await getEmDashEntry(item.collection, item.slug ?? item.id, { locale: item.locale });
return !!entry && getContentSeo(entry)?.noIndex !== true;
}),
);
const items = found.filter((_, i) => shown[i]);
---
<Base title="Search">
<div class="frame">
<SearchResults query={query}>
<form method="get" action="/search" role="search">
<input type="search" name="q" value={query} aria-label="Search" />
<button>Search</button>
</form>
<ol>
{items.map((item) => <li><a href={entryUrl(item)}>{item.title ?? item.slug}</a></li>)}
</ol>
</SearchResults>
</div>
</Base>
frame stands for the theme’s own container, and entryUrl for its own link helper. Use your theme’s links: a collection’s URL pattern, locale prefix or trailing slash can differ from /collection/slug, so a link built by hand may 404. The noIndex check keeps entries out of the list that the search index leaves out too.
What you put inside SearchResults is the backup. Here that is EmDash’s own search(), its database full-text search. It shows when the script does not run. Once the index loads, our results page draws over it, and the backup stops showing. So the page never shows two search fields. If anything fails, the backup stays.
Do not leave the theme’s form outside SearchResults. Our results page draws its own search field, so the page would show two.
SearchResults takes these props:
| Prop | What it does |
|---|---|
query |
The query the server searched for. Pass it, so the page starts with the same query. |
index |
The index to search. Default: the one in the URL, then the site’s. |
types |
The types the page shows, as a list or a comma-separated string. Default: all. |
class |
Your class names. |
The Results page URL setting in the admin page must match this page’s URL, and the theme form’s action, exactly. Trailing slash included. The default is /search.
On any server
On a site that loads the search embed by hand, print a results 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>
The search loads its results code at once on a page with a mount. Once the index is loaded, the results page draws in a shadow root, and your list stops showing. If anything fails, your list stays.
| Attribute | What it does |
|---|---|
data-search-results |
Marks the mount. Required. |
data-search-query |
The server’s query, for pretty URLs such as /search/lighthouse/. It applies only to the first history entry. |
data-search-index |
The index. The URL’s index parameter comes first, then this, then the config’s index. |
data-search-types |
A comma-separated list of the types the page shows. |
The same page is a module too. mountResultsPage(root, config) from @astronomic-dev/search/results draws it into root and returns an unmount function. After an unmount, your list shows again.
For the embed itself, see Embed on any server.
How it looks
This part is for the EmDash and any-server results page. SearchPage on Astro is plain markup that your own CSS styles.
The results page has no colors of its own, so it matches your theme. It takes the page’s font and text color. Lines, fills, matched words and the Search button come from the text color.
The Search button is an outline in the text color. To fill it with your brand color, set --search-accent on the mount or a parent. Its text is then white or black, whichever contrasts with the accent. To pick the text color yourself, set --search-accent-fg:
.frame {
--search-accent: #e8590c;
--search-accent-fg: #fff;
}
The theme’s own CSS for ol, li, a or button does not reach the results, because they draw in a shadow root. Style them with the --search-* custom properties and ::part(). See Look and text.
On this page, Back and forward work as you expect. Each change to the query replaces the history entry, and a submit adds one. So Back returns to the last search you submitted.
URL names
These names apply to the EmDash and any-server results page. The results URL holds the query, the type tab, the index and each filter pick:
| Part | Name |
|---|---|
| Query | queryParam, default q |
| Type tab | ${paramPrefix}type |
| Index | ${paramPrefix}index |
| A filter pick | ${paramPrefix}<facet> |
paramPrefix is empty by default, so the names are q, type, index and the facet’s name. “See all results” in the dialog always writes the index, so the results page searches the same one.
The query, type and index names are reserved. A facet with one of those names is not read from or written to the URL. A queryParam equal to the type or index name falls back to q.
On the embed, queryParam and paramPrefix are fields of the config tag. Set paramPrefix when your site already uses short names such as tag or paged for its own parameters. See Embed on any server. On EmDash, they are fixed: q and no prefix.
Next: Works with your theme.