More Articles

How to Easily Add Search Functionality to Eleventy Static Sites with Pagefind

Discover how to quickly integrate Pagefind, a lightweight and performant static search library, into your Eleventy static site for a seamless search experience.

Written on Jun 6, 2024 | About 1 min reading time

New tools in your inbox every Monday!

How to Easily Add Search Functionality to Eleventy Static Sites with Pagefind

Adding search functionality to static sites can be challenging, often requiring complex setups or relying on third-party services. Pagefind offers a simple, lightweight solution that seamlessly integrates with popular static site generators like Eleventy.

What is Pagefind?

Pagefind is a fully static search library designed to perform well on large sites while minimizing bandwidth usage and avoiding the need for hosted infrastructure. Its ease of use and impressive capabilities make it a go-to choice for developers building static sites.

Getting Started with Pagefind for Eleventy

Install Pagefind

Install the Pagefind package using npm:

npm install --save-dev pagefind

Build the Search Index

Add the data-pagefind-body attribute to the element containing your post content in your Eleventy templates:

<article data-pagefind-body>{{ content | safe }}</article>

Configure Eleventy to run the Pagefind CLI after your site has been built:


  const { execSync } = require('child_process')

  eleventyConfig.on('eleventy.after', () => {
    execSync(`npx pagefind --source dist --glob \"**/*.html\"`, { encoding: 'utf-8' })
  })

Include the Pagefind stylesheet and optional dark mode styles. Add an element for the search box:

<link href="/_pagefind/pagefind-ui.css" rel="stylesheet">

  <div id="search"></div>

  <script src="/_pagefind/pagefind-ui.js" onload="new PagefindUI({ element: '#search' });"></script>

Elevate Your Static Site with Pagefind

With minimal effort, you've added powerful search functionality to your Eleventy site, ensuring visitors can easily find the content they're looking for. Pagefind's seamless integration and lightweight nature make it an excellent choice for static sites. Enhance your user experience without sacrificing performance or adding complex dependencies. For more information including filtering, sorting, and more complex indexing options visit Pagefind's docs at https://pagefind.app.

September Featured Tools

Good Palette

Try out different color schemes on various UI layouts.

Explore

Optimize Images

9 Image Optimization Tools In One Single Toolkit

Explore

We Recommend...

Top products featured for the article.

Palette HQ

Stop spending hours adjusting spreadsheets to figure out sales commissions. Our sales compensation software helps you design and manage sales commission plans built for your growth and company goals.

Explore

Turbo Framework

The speed of a single-page web application without having to write any JavaScript.

Explore

AdminJS

Open-Source Admin Panel for your Node.js Application

Explore

Copy.ai

Forget writers block. Get blog posts, ads, social media content, poems, business ideas and more by just clicking a button. Our bots will write everything for you.

Explore

Sponsored

See all Tools

A Little Extra Reading

Feel like reading more? Here are some extra articles to check out!

See all Articles