Kevin Green
Developer at Sanity
Automatically update your Algolia index
import {env} from 'node:process'
import {documentEventHandler} from '@sanity/functions'
import {algoliasearch} from 'algoliasearch'
const {ALGOLIA_APP_ID = '', ALGOLIA_WRITE_KEY = ''} = env
// TODO: Allow this function to run on multiple indexes/post types (e.g. 'posts', 'products', 'events', etc.)
const ALGOLIA_INDEX_NAME = 'posts'
export const handler = documentEventHandler(async ({event}) => {
const {_id, title, hideFromSearch, operation} = event.data
const algolia = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_WRITE_KEY)
if (operation === 'delete') {
try {
// We are assuming you already have an algolia instance setup with an index called 'posts'
// addOrUpdateObject documentation: https://www.algolia.com/doc/libraries/javascript/v5/methods/search/delete-object/?client=javascript
await algolia.deleteObject({
indexName: ALGOLIA_INDEX_NAME,
objectID: _id,
})
console.log(`Successfully deleted document ${_id} ("${title}") from Algolia`)
} catch (error) {
console.error('Error syncing to Algolia:', error)
throw error
}
} else {
try {
// We are assuming you already have an algolia instance setup with an index called 'posts'
// addOrUpdateObject documentation: https://www.algolia.com/doc/libraries/javascript/v5/methods/search/add-or-update-object/?client=javascript
await algolia.addOrUpdateObject({
indexName: ALGOLIA_INDEX_NAME,
objectID: _id,
body: {
title,
hideFromSearch, // This is an optional field that you can use to hide a document from search results
},
})
console.log(`Successfully synced document ${_id} ("${title}") to Algolia`)
} catch (error) {
console.error('Error syncing to Algolia:', error)
throw error
}
}
})import {defineBlueprint, defineDocumentFunction} from '@sanity/blueprints'
import 'dotenv/config'
import process from 'node:process'
const {ALGOLIA_APP_ID, ALGOLIA_WRITE_KEY} = process.env
if (typeof ALGOLIA_APP_ID !== 'string' || typeof ALGOLIA_WRITE_KEY !== 'string') {
throw new Error('ALGOLIA_APP_ID and ALGOLIA_WRITE_KEY must be set')
}
export default defineBlueprint({
resources: [
defineDocumentFunction({
type: 'sanity.function.document',
name: 'algolia-document-sync',
memory: 1,
timeout: 10,
src: './functions/algolia-document-sync',
event: {
on: [
"create",
"update",
"delete"
],
filter: "_type == 'post'",
projection: "{_id, title, hideFromSearch, 'operation': delta::operation()}"
},
env: {
ALGOLIA_APP_ID,
ALGOLIA_WRITE_KEY
},
}),
],
})Keeping your search index updated is a critical component to any large site utilizing things like Algolia. This function removes some of the technical debt that might be associated to cron jobs, or even web hooks that trigger Algolia updates remotely. This function reduces that complexity by updating your index on publish, update and delete for articles, products, whatever needs to be stored in Algolia.
Quick Start
View the complete example and source code.
Initialize blueprints if you haven't already: npx sanity blueprints init
Then: npx sanity blueprints add function --example algolia-document-sync
Then deploy: npx sanity blueprints deploy
How It Works
When a content editor publishes a new blog post, the function automatically:
Key Benefits
Technical Implementation
The function syncs data to Algolia to keep your search index updated:
Perfect For
The function is compatible with any Sanity project and can be easily customized to manage other content types, post more data to Algolia, or handle other index update requirements.
Developer at Sanity
Automatically track when content was first published with a timestamp that sets once and never overwrites, providing reliable publication history for analytics and editorial workflows.
Go to First Published Timestamp FunctionAI-powered automatic tagging for Sanity blog posts that analyzes content to generate 3 relevant tags, maintaining consistency by reusing existing tags from your content library.
Go to Automatically tag blog postsOn document publish, send a Slack notification so your team is informed
Go to Notify your team via Slack on publishCreate, preview, and send Klaviyo campaigns without ever leaving Sanity Studio"
Go to Klaviyo campaigns without leaving your Studio