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

sanity-algolia

v1.1.0

Published

Here are some helpers to facilitate indexing your Sanity documents as Algolia records, via custom serializing, optional hidden/visibility filtering and directly syncing to an Algolia index from a Sanity webhook.

Downloads

19,825

Readme

Sanity + Algolia = ♥️

Here are some helpers to facilitate indexing your Sanity documents as Algolia records, via custom serializing, optional hidden/visibility filtering and directly syncing to an Algolia index from a Sanity webhook.

Steps to implement

  1. Set up one or more webhooks on Sanity to trigger when content is created, edited or deleted.
  2. Deploy a serverless function to receive the webhook payload and update Algolia index

Setup webhook

This is an example of indexing Sanity content in Algolia directly from a Sanity webhook. The target of the webhook is a serverless function running on Vercel, Netlify, AWS etc.

Set up the following Webhook on Sanity. It needs to be that specific webhook projection at the moment, as this module is not yet updated to take advantage of the new GROQ powered Webhooks. You should configure your webhook to target the URL of the serverless function once deployed.

Deploy serverless function

See examples in the example/ folder in this repository for how this can be done. You will want to tailor this code to your own content types and fields, and do any transformation on your data before its indexed in Algolia.

Note that your serverless hosting might require a build step to properly deploy your serverless functions, and that the exported handler and passed parameters might differ from the included examples. Please refer to documentation on deploying functions at your hosting service of choice in order to adapt it to your own needs.

First time indexing

The webhook is great for keeping Algolia up to date to new changes in your Sanity datasets, but you likely also want to first index any content you already have. The simplest way to do this is to run the sanityAlgolia.webhookSync method manually. For ease of use you can export the sanity client and the sanityAlgolia objects from your handler file exemplified above and make use of them like this

const sanity = ...; // configured Sanity client
const sanityAlgolia = ...; // configured sanity-algolia

// Fetch the _id of all the documents we want to index
const types = ["article", "page", "product", "author"];
const query = `* [_type in $types && !(_id in path("drafts.**"))][]._id`

sanity.fetch(query, { types }).then(ids =>
  sanityAlgolia.webhookSync(sanity, { ids: { created: ids, updated: [], deleted: [] }})
)

Links