#Search capability for Static Site

25 messages · Page 1 of 1 (latest)

azure wigeon
#

Hello Community :),

While researching to create "Search" capability for my static site, I have found two main libraries: Lunr (https://github.com/olivernn/lunr.js/) and Stork (https://github.com/jameslittle230/stork).

Search Functionality:
Following is how I anticipate integrating search into my static site

  1. Locally, build an index (JSON) file against my blogs (mds, astro etc files). Hopefully via tooling provided by one of these libraries.
  2. On the website, only load this JSON file when user clicks the search button.
  3. Then in browser, leverage Lunr or Stork or any community suggested library to search against the JSON index file.

Question:
1/ Does anyone have experience with either of these? If so which one do you recommend?
2/ Any existing Astro integration or other library to check out for Search capability?

Thank you for your help and time!

faint oasis
lusty sonnet
rotund folio
azure wigeon
#

Thank you @faint oasis , @lusty sonnet , @rotund folio 🙂 .

I will checkout these valuable resources.

vestal mesa
hushed ginkgo
vestal mesa
earnest basalt
#

@vestal mesa @hushed ginkgo I'm implementing Pagefind on my site, and curious if there’s a way to integrate it with astro dev the way you did with Hugo in your write-up, Bryce. Did either of you find a way to get Pagefind working in dev mode (other than watching /dist from a recent build)?

vestal mesa
hushed ginkgo
earnest basalt
hollow tinsel
hollow mauve
#

Lunr does work well enough, but sadly it seems that it is currently unmaintained as the last update was some time in 2020.

formal patrol
#

https://www.algolia.com/ Algolia is what they use on most of these documentation sites, including Astro's docs. It's a 3rd party service that can just index your site for you or you can supply indexes to it.

Algolia

Create AI-powered search & discovery across websites & apps.

hushed ginkgo
hollow mauve
#

From looking at the documentation Pagefind looks pretty incredible. I'll give this integration a try

hushed ginkgo
#

Yeah, it's great. If you need any help on how to override the styles, let me know. He has examples of the css variables, but I've found it necessary to change some more styles by class name.

stiff lily
#

@hushed ginkgo could you point to an example of these class-overrides?

#

Otherwise did anybody manage to write their own search component with pagefind

hollow mauve
# stiff lily Otherwise did anybody manage to write their own search component with pagefind

I will try to write one with Solidjs I think, mainly because I would like to use Pagefind with some search UI I already have. I haven't started yet, but Pagefind docs have some information about it
https://pagefind.app/docs/api/

Pagefind

Pagefind can be accessed as an API directly from JavaScript, for you to build custom search interfaces, or integrate with existing systems and components.
Initializing Pagefind Anywhere on your site, you can initialize Pagefind with:

hollow mauve
#

I wrote my own rudimentary types for Pagefind to use in TypeScript:

interface Pagefind {
  search: (query: string) => Promise<PagefindResponse>;
}

interface PagefindResult {
  id: string;
  data: () => Promise<PagefindDocument>;
}

interface PagefindResponse {
  results: PagefindResult[];
}

interface PagefindDocument {
  url: string;
  excerpt: string;
  filters: {
    author: string;
  };
  meta: {
    title: string;
    image: string;
  };
  content: string;
  word_count: number;
}

I was able to import pagefind like this (it avoids Vite headaches)

async function loadPagefind(): Promise<Pagefind> {
  const pf = "/_pagefind/pagefind.js";
  return await import(/* @vite-ignore */ pf);
}

I use Solidjs so I load it when the component mounts:

export default function Search() {
  const [query, setQuery] = createSignal('');

  // Search
  let pagefind: Pagefind;
  const [pages] = createResource(query, async (query: string) => await pagefind.search(query));
  
  // == snip ==

  onMount(async () => {
    pagefind = await loadPagefind();
    // == snip ==
  }
  
  return (
    <div>jsx here</div>
  )
}
#

If someone finds a better way to use Pagefind in Vite please let me know 😅