{"id":306624,"date":"2020-04-22T07:54:48","date_gmt":"2020-04-22T14:54:48","guid":{"rendered":"https:\/\/css-tricks.com\/?p=306624"},"modified":"2021-08-18T07:47:08","modified_gmt":"2021-08-18T14:47:08","slug":"how-to-add-lunr-search-to-your-gatsby-website","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/how-to-add-lunr-search-to-your-gatsby-website\/","title":{"rendered":"How to Add Lunr Search to Your Gatsby Website"},"content":{"rendered":"\n
The Jamstack way<\/a> of thinking and building websites is becoming more and more popular.<\/p>\n\n\n\n Have you already tried Gatsby,<\/a> Nuxt,<\/a> or Gridsome<\/a> (to cite only a few)? Chances are that your first contact was a \u201cWow!\u201d moment \u2014 so many things are automatically set up and ready to use. <\/p>\n\n\n\n There are some challenges, though, one of which is search functionality. If you\u2019re working on any sort of content-driven site, you\u2019ll likely run into search and how to handle it. Can it be done without any external server-side technology? <\/p>\n\n\n\n Search is not<\/em> one of those things that come out of the box with Jamstack. Some extra decisions and implementation are required.<\/p>\n\n\n\n\n\n\n\n Fortunately, we have a bunch of options that might be more or less adapted to a project. We could use Algolia\u2019s<\/a> powerful search-as-service API. It comes with a free plan that is restricted to non-commercial projects with a limited capacity. If we were to use WordPress with WPGraphQL<\/a> as a data source, we could take advantage of WordPress native search functionality and Apollo Client.<\/a> Raymond Camden recently explored a few Jamstack search options<\/a>, including pointing a search form directly at Google.<\/p>\n\n\n\n In this article, we will build a search index and add search functionality to a Gatsby website with Lunr<\/a>, a lightweight JavaScript library providing an extensible and customizable search without the need for external, server-side services. We used it recently to add \u201cSearch by Tartan Name\u201d to our Gatsby project tartanify.com<\/a>. We absolutely wanted persistent search as-you-type functionality, which brought some extra challenges. But that\u2019s what makes it interesting, right? I\u2019ll discuss some of the difficulties we faced and how we dealt with them in the second half of this article.<\/p>\n\n\n\n