astro-pagefind

repository·main·Indexed 19 days ago

https://github.com/shishkin/astro-pagefind

An Astro integration for Pagefind, a static site search library. It automates index generation during builds and provides Astro components, such as Search and PagefindConfig, to embed search UI into Astro projects. The integration requires Astro's output mode to be set to static.

Tokens
1.8K
Snippets
9
Records
11
Agent score
66%

What's inside astro-pagefind

  1. Integrate Pagefind UI components directly (Recommended)

    main

    For Pagefind 1.5.0+, it is recommended to use Pagefind's native component-based UI instead of the Search.astro component. To ensure the Pagefind bundle path is wired correctly in Astro, you must include the PagefindConfig component on your page.

    To implement a searchbox, import PagefindConfig and use the <pagefind-searchbox> web component.

    ---
    import PagefindConfig from "astro-pagefind/components/PagefindConfig.astro";
    ---
    
    <PagefindConfig />
    <pagefind-searchbox></pagefind-searchbox>
  2. Configure the astro-pagefind integration

    main

    Add astro-pagefind to the integrations array in your astro.config.ts file to enable index building during static builds and serving prebuilt indices in astro dev mode.

    //astro.config.ts
    
    import { defineConfig } from "astro/config";
    import pagefind from "astro-pagefind";
    
    export default defineConfig({
      integrations: [pagefind()],
    });
  3. Initialize astro-pagefind in astro.config.ts

    main

    To use astro-pagefind in your Astro project, import the default export from astro-pagefind and add it to the integrations array in your astro.config.mjs or astro.config.ts file.

    import astroPagefind from 'astro-pagefind';
    import { defineConfig } from 'astro/config';
    
    export default defineConfig({
      integrations: [astroPagefind()],
    });
  4. Use the Search Astro component

    main

    You can use the Search component from astro-pagefind/components/Search.astro. Note that this component is currently in maintenance mode and will not receive new UI features. It is useful for simple integrations where you want to pass specific searchboxOptions via props.

    Props:

    • instance: A unique identifier for the search instance.
    • className: CSS class for the component.
    • searchboxOptions: An object containing options to pass to the Pagefind Searchbox.
    ---
    import Search from "astro-pagefind/components/Search.astro";
    ---
    
    <Search instance="search" className="pagefind-ui" searchboxOptions={{ placeholder: "search" }} />
  5. Use the Search component in Astro

    main

    The Search component is the primary entry point for rendering the Pagefind search UI in an Astro project. It wraps the <pagefind-searchbox> web component and allows you to pass configuration for both the Pagefind engine and the search box UI via props.

    To use it, import Search from astro-pagefind/components/Search.astro and provide optional props to customize its behavior and appearance.

    ---
    import Search from "astro-pagefind/components/Search.astro";
    ---
    
    <Search 
      instance="my-search-instance" 
      className="custom-search-class" 
      searchboxOptions={{ placeholder: "Search articles..." }}
      configOptions={{ libPath: "/pagefind/" }}
    />
  6. Configure the astro-pagefind integration

    main

    To use astro-pagefind, add it to your astro.config.mjs file. You can optionally provide an indexConfig object, which is passed directly to Pagefind's createIndex function to customize how the search index is generated.

    Note: This integration requires your Astro output mode to be static. If your config.output is set to server, the integration will issue a warning because it requires static *.html pages to function.

    import astroPagefind from 'astro-pagefind';
    
    export default defineConfig({
      integrations: [
        astroPagefind({
          indexConfig: {
            // PagefindServiceConfig options go here
          }
        })
      ],
    });
  7. Configure PagefindConfig props

    main

    The PagefindConfig component accepts several props which are passed directly to the underlying <pagefind-config> web component.

    Props

    • instance (optional): A string identifying a specific Pagefind instance. This allows you to have multiple independent search instances on a single page.
    • ...props: Any other valid attributes for the <pagefind-config> web component (e.g., configuration settings for the Pagefind engine).
  8. PagefindOptions interface

    main

    The PagefindOptions interface defines the configuration available when initializing the astro-pagefind integration.

    PropertyTypeDescription
    indexConfigPagefindServiceConfigConfiguration options passed directly to the underlying pagefind createIndex method.
    export interface PagefindOptions {
      /**
       * `PagefindServiceConfig` passed to pagefind's `createIndex`
       */
      indexConfig?: PagefindServiceConfig;
    }
  9. Configure the Search component props

    main

    The Search component accepts the following props:

    PropTypeDescription
    instancestring (optional)A unique identifier for the search instance. Useful if you have multiple search boxes on one page.
    classNamestring (optional)CSS class name applied to the <pagefind-searchbox> element.
    searchboxOptionsRecord<string, unknown> (optional)An object containing options passed directly to the <pagefind-searchbox> web component. Defaults to {}.
    configOptionsRecord<string, unknown> (optional)An object containing configuration options passed to the PagefindConfig component to initialize the Pagefind engine. Defaults to {}.
  10. Use the PagefindConfig Astro component

    main

    The PagefindConfig component is used to inject Pagefind configuration into your Astro page. It renders a <pagefind-config> web component and automatically handles the bundle-path by pointing it to the pagefind/ directory relative to your BASE_URL.

    It also ensures that the @pagefind/component-ui library is imported in the client-side script to initialize the search UI.

    import PagefindConfig from "astro-pagefind/components/PagefindConfig.astro";
    
    // Basic usage
    <PagefindConfig />
    
    // Usage with a specific instance
    <PagefindConfig instance="my-search-instance" />