· 2 min read
Search with Pagefind
Added search: a magnifying-glass button in the header opens a box under it (a popovertarget popover, like the colour theme menu). / opens it from anywhere.
What we did
- Added search: a magnifying-glass button in the header opens a box under it (a
popovertargetpopover, like the colour theme menu)./opens it from anywhere. - Pagefind 1.6.0 runs after both Vite builds (
pagefind --site build) and writes a static index intobuild/pagefind/, one per<html lang>. A search downloads only the slices it needs; no server, so it works on GitHub Pages. - Only elements marked
data-pagefind-bodyare indexed (posts, Devlog entries, project headers, Awesome lists, the About intro), each withdata-pagefind-meta="section:…"for the label over a result. Date lines, tag lists and code-block labels aredata-pagefind-ignore: they made the excerpts noisy. search.tsimportspagefind.js?lang=…at run time: a different URL is a different module, so switching language (which doesn't reload the page) gets the other index.- The box animates in and out (
@starting-styleandallow-discrete), the results stagger in, and a result is a morph card: its title and excerpt fly into the page's header. pnpm previewand the e2e tests now servebuild/withscripts/serve.ts, which copies GitHub Pages' rules.
What I learned
vite previewserves.svelte-kit/output, notbuild/: anything added after SvelteKit (the search index, the CMS at/admin) isn't there. That's why search "didn't work on preview".- A popover closes instantly by default, because
displaycan't be animated;transition-behavior: allow-discreteondisplayandoverlaykeeps it on screen until the fade ends. - The search box stays in the page while the page behind it changes, so the result would still be there in the transition's "after" picture with the same name as the new heading, and the browser would skip the transition.
leave()inmorph.tsunmarks it and closes the box without its animation (which also cancels one already running). - Svelte drops CSS rules for attributes set from JavaScript (
data-instant) unless they're:global(...).
Next
- Maybe filters by section in the box.