npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

netlify-plugin-search-index

v0.1.6

Published

Generate a Search Index you can query via JavaScript or a Netlify Function

Downloads

274

Readme

Netlify Search Index Plugin

Generate a Search Index you can query via a static JSON blob or a Netlify Function!

You may not need this - There are other ways to add search to your site, like using Algolia or Vanilla JS with a custom search Index.

However, you may wish to have a way to generate this index based ONLY on crawling your generated static site, or you may wish to do index searches in a serverless function instead of making your user download the entire index and run clientside.

Demo

  • Demo site: https://netlify-plugin-search-index.netlify.app
  • Demo search function: https://netlify-plugin-search-index.netlify.app/.netlify/functions/search?s=web
  • Demo JSON blob: https://netlify-plugin-search-index.netlify.app/searchIndex.json

Usage

To install, add the plugin in your netlify.toml. No config is required but we show the default options here.

[[build]]
  functions = functions # must specify a functions folder for this to work
[[plugins]]
  package = netlify-plugin-search-index
    # all inputs is optional, we just show you the defaults below
    # [plugins.inputs]
      # ignore = ["/ignore-this-file.html"] # don't index this file
      # generatedFunctionName = search # change the name of generated folder in case of conflicts, use `null` to turn off
      # publishDirJSONFileName = searchIndex # also use null to turn off

Without config, this would generate:

  • a function at https://yoursite.netlify.com/.netlify/functions/search and
  • a clientside JSON blob at https://yoursite.netlify.com/searchIndex.json

To use this plugin only for the generated serveless function, supply null to the publishDirJSONFileName:

[[plugins]]
  package = netlify-plugin-search-index
    [plugins.inputs]
      generatedFunctionName = mySearchFunction
      publishDirJSONFileName = null

This would generate a Netlify function at https://yoursite.netlify.com/.netlify/functions/mySearchFunction which you can query with https://yoursite.netlify.com/.netlify/functions/mySearchFunction?search=foo.

To use this plugin only for the clientside JSON file, supply null to the generatedFunctionName:

[[plugins]]
  package = netlify-plugin-search-index
    [plugins.inputs] = 
      generatedFunctionName = null
      publishDirJSONFileName = mySearchIndex # you can use / to nest in a directory

This would generate a clientside JSON at https://yoursite.netlify.com/mySearchIndex.json.

Supplying null to both generatedFunctionName and publishDirJSONFileName would be meaningless (because there would be nothing to generate) and cause an error.

More options

Exclude files

Your project probably contains some content files that you don't want your users to search. Pass an array of paths (or regex) to the files you don’t want to be indexed to dismiss them:

[[plugins]]
  package = netlify-plugin-search-index
    [plugins.inputs] = 
      exclude = ['/ignore-this-file.html', /^\/devnull\/.*/]

Search params

At the moment of writing, it is not possible to pass custom options to FuseJS. If it's something that you would like to see implemented, let us know! ✌️

What It Does

After your project is built:

  • this plugin goes through your HTML files
  • extracts their metadata (title, description, keywords) with unified
  • converts them to searchable content, weighted by field type
  • stores them as a JSON blob in /searchIndex/searchIndex.json
  • generates a Netlify Function that fuzzy searches against a query string with fuse.js

You can use this plugin in two ways:

  • Client-side: You can simple require the JSON blob in your clientside JavaScript if it isn't too big:
    // app.js
    import searchIndex from './searchIndex.json'
  • Serverless-side: You can use the generated function that reads the JSON and returns fuzzy search results to be lighter on your frontend. The generated function is available at .netlify/functions/searchIndex and you can use it with a search term like .netlify/functions/searchIndex?s=foo or .netlify/functions/searchIndex?search=foo:
    // app.js
    document.getElementById('myForm').addEventListener('submit', async event => {
      event.preventDefault()
      const result = await fetch(`/.netlify/functions/searchIndex?search=${event.target.searchText.value}&limit=25`).then(x => x.json())
      document.getElementById('result').innerText = JSON.stringify(result, null, 2)
    })

You can use an optional limit parameter to limit the length of returned results.

Under the hood, the search function uses fuse.js and in future we may expose more configurations for this.

Notes for contributors

We had to use patch-package to fix this bug: https://github.com/paulmillr/readdirp/issues/157 - readdirp is a dependency of ``copy-template-dir` which we use.

Hopefully it will be resolved or we can just fork copy-template-dir entirely locally.

Future plans

WE ARE SEEKING MAINTAINERS.

Micro Todo:

  • expose fuse.js and html parse search options for more configurability
  • filtering results
  • support non html files?