Search

npm run build writes a search index under site/api/ and bundles a React runtime that mounts into the <Search /> placeholder or [data-canopy-search]. Works (IIIF manifests) and pages (MDX files) share a single FlexSearch store so filters and type tabs stay in sync.

Data sources

  • Works — every manifest pulled from collection URIs becomes a type: 'work' record with id, title, and href pointing at /works/<slug>.html. Metadata fields listed in search.index.metadata are flattened for indexing.
  • Pages — MDX files contribute type: 'page' records when they have no front matter or when they explicitly set type (or searchType). Add search: false to opt out. Optional searchSummary and searchSummaryMarkdown fields override the teaser, and description does where neither is set; image shows beside the result (Customize search).

A page is found by its prose: its headings, paragraphs, lists, quotes and table cells, and the words its components hold. Code blocks, imports, {expressions} and the attributes of its components are left out. Without a teaser, its result shows the passage around the words searched for.

packages/app/lib/build/search-index.js combines the records and keeps IDs stable between search-index.json (FlexSearch input) and search-records.json (display data).

How a search matches

The search page and the header's field find the same things, the same way:

  • A word is found by its beginning: manu finds "manuscript", ma finds "map".
  • Case and accents do not matter: haridwar finds "Haridwār", and México finds "Mexico".
  • Every word of a query must be found: seated woman finds only what has both words.
  • A record is found by its title, then its metadata, summary and annotation, and a match in the title ranks first.

A visitor's browser builds the index the first time they search and keeps it until the site's index changes, so later pages search at once.

The header's field

The search field in the header suggests works and pages as a visitor types, grouped by type, and says how many it found to screen readers. The down arrow moves into the suggestions, the up arrow and Escape come back to the field, and Enter on the field opens the search page. ⌘K (Ctrl K on Windows and Linux) goes to the field from anywhere on the page. A page loads nothing for search until its field is used.

On the search page itself, the field drives the page's results instead.

Page composition

content/search/_layout.mdx lays out the search page, and every site needs one (the templates ship it). Place the search components in it anywhere, in any markup: <SearchTabs />, <SearchSummary /> and <SearchResults /> read from the same results. The header's search field drives them. The layout receives the page's details as props.page, its title among them.

content/search/_layout.mdx
<div className="canopy-search-results">  <h1>{props.page?.title || "Search"}</h1>  <SearchTabs />  <SearchSummary />  <SearchResults /></div>

Open the layout with an h1, as above: each page's result is titled by a heading one level below the last heading before the results, h2 under the page's h1. Set it yourself with <SearchResults headingLevel={3} />.

Page metadata

Configure the browser <title> and meta description via search.page inside canopy.yml. A search page in another of the site's languages takes that language's word for "search" from its locale.yml (common.nouns.search) as its title.

canopy.yml
search:  page:    title: Discover everything    description: Browse every work and story in one place.

Tabs, layouts, and templates

Control the rendered tabs and markup via search.results in canopy.yml:

canopy.yml
search:  results:    work:      layout: grid # grid | list      result: figure # figure | article    page:      layout: list      result: article  tabs:    order:      - work      - page
  • layout switches between the masonry grid (uses react-masonry-css) and a stacked list.
  • result picks the MDX partial that renders each item. Define _result-figure.mdx or _result-article.mdx under content/search/ to override the defaults. Each file receives the record (props.record), the active query, and helper props for badges or metadata.
  • tabs.order reorders tabs without redefining layout/result. Types omitted from results inherit the default grid + figure combo.

The tabs are one stop for the keyboard: the left and right arrows, Home and End move between them, and the results follow at once.

Filtering and URL state

The runtime watches ?q and ?type query params, so deep links remain stable when you switch between tabs or refine a search. When you add more record types via front matter (searchType: example), include them in search.results and tabs.order to expose dedicated tabs.

Styling hooks

  • Use the .canopy-search class (root element) to add custom spacing or background treatments in app/styles/main.css.
  • Override button or input styles globally via the Tailwind preset or your own CSS variables in @theme.
  • When you need different markup for specific record types, create additional templates under content/search/ and branch based on record.type inside the MDX.

Debugging tips

  • Inspect site/api/search-index.json to confirm front-matter summaries and metadata labels are being indexed.
  • When the browser console logs Dynamic require of 'react' is not supported, make sure the UI build marks react, react-dom, react-dom/client, react-masonry-css, and flexsearch as externals (the repository is already configured this way).
  • Delete .cache/search (if present) plus site/api/search-* and rebuild when you change record shapes.
  • When the search page says "Search unavailable.", its runtime could not load the index; the browser console says why.
  • Add ?searchDebug to the search page's address to log how long the index took to build, or to load from the browser's cache.

Metadata browse

Canopy builds /metadata automatically. It groups the metadata labels you list in canopy.yml (metadata array) and renders links to every unique value found across the ingested manifests. Each link jumps directly to search results filtered to that label/value pair.

canopy.yml
metadata:  - Subject  - Creator  - Date  - Location

During the IIIF build, Canopy generates /api/facet/<label>/<value>.json collections for those labels. The metadata page uses the files to calculate counts and to build the links used elsewhere (home sliders, related items, search filters).

Page anatomy

  1. Intro copy — content/metadata/index.mdx controls the opening paragraph and any contextual guidance you want to provide to visitors.
  2. Facet sections — For each configured label Canopy shows the heading and a list of values, ordered by descending occurrence. Each value links to /search?type=work&label=<label>&value=<value>.
  3. Counts — Values display the number of works that match the label/value pair so users can gauge scale before clicking through.

Editing tips

  • Keep facet labels short and consistent; the UI uses the label verbatim as the section heading.
  • Remove labels from canopy.yml when you stop using them to avoid generating unused /api/facet/** files.
  • Pair metadata browse sections with MDX essays that explain curatorial choices. Cite those works via referencedManifests so the relationship flows back to individual work pages.