Sanity Webhooks for Cache Invalidation
Set up automatic cache invalidation when content is updated in Sanity.
Setup
- Go to Sanity Manage
- Select your project
- Go to API → Webhooks
- Click Create webhook
Configuration
Configure the webhook with:
- Name: Cache Invalidation
- URL:
https://your-domain.com/api/webhooks/sanity - Dataset: production (or your dataset)
- Trigger on: Create, Update, Delete
- HTTP method: POST
- Secret: Set
SANITY_WEBHOOK_SECRETin your env - HTTP Headers:
Authorization: Bearer ${SANITY_WEBHOOK_SECRET}
Testing
Test the webhook manually:
curl -X POST https://your-domain.com/api/webhooks/sanity \
-H "Authorization: Bearer your-secret" \
-H "Content-Type: application/json" \
-d '{"_type": "hero"}'
How It Works
- Content is published in Sanity
- Sanity sends webhook to your API
- API validates the webhook secret
- Cache is automatically cleared
- Next request fetches fresh content