Triggering Netlify redeploy from Sanity and setting up live preview in Next.js with Sanity.

24 replies
Last updated: Aug 18, 2022
I love Sanity and have been using it for the content for a site I have deployed to Netlify. My question: is there a way to trigger a Netlify redeploy from Sanity when my content changes? I see a dashboard widget -- "Netlify Deploy" -- and a plugin -- "Netlify Deploy Status Badge" -- which both appear to do the trick. But I'm looking for feedback. best practices, and where I can look to find a 'how to...' Thanks!
AI Update

Great question! Yes, you can absolutely trigger a Netlify redeploy when your Sanity content changes. Let me walk you through your best options.

The Standard Approach: GROQ-Powered Webhooks

The most reliable, production-ready way to trigger Netlify builds is using Sanity's GROQ-powered webhooks. This is the official, mature feature designed exactly for this use case. Here's how it works:

  1. Get your Netlify build hook URL: In Netlify, go to Site Settings → Build & deploy → Build hooks, and create a new build hook. You'll get a URL like https://api.netlify.com/build_hooks/YOUR_HOOK_ID

  2. Set up the webhook in Sanity: Go to your Sanity project dashboard → API → Webhooks → Create webhook

  3. Configure it:

    • URL: Your Netlify build hook URL
    • Trigger on: Create, Update (and Delete if needed)
    • Filter: Use GROQ to control when it fires. For example, !(_id in path("drafts.**")) will only trigger on published documents, not drafts
    • HTTP method: POST

The webhooks documentation has excellent guides on using filters and projections to fine-tune exactly when your webhook fires.

The Dashboard Widget: Manual Control

The Netlify plugin you mentioned (also known as "Netlify Deploy" or sanity-plugin-netlify) is perfect if you want manual control over deployments. It adds a deploy tool to your Studio where editors can:

  • Trigger builds on demand with a button click
  • View deployment history and status
  • Manage multiple Netlify sites
  • See live deployment status updates

Install it with:

npm install sanity-plugin-netlify

Then add it to your sanity.config.ts:

import { defineConfig } from 'sanity'
import { netlifyTool } from 'sanity-plugin-netlify'

export default defineConfig({
  // ...
  plugins: [
    netlifyTool(),
  ],
})

This approach is great for avoiding unnecessary builds on every content save, and gives content editors visibility into deployment status directly in Studio.

Best Practices

Avoid build storms: Don't trigger on every single save. Consider:

  • Using GROQ filters like !(_id in path("drafts.**")) to only trigger on published documents
  • Filtering by specific document types: _type == "post"
  • Using the dashboard widget for manual control instead of automatic webhooks
  • Being mindful of Netlify's build minute limits

Security: Keep your Netlify build hook URL private - anyone with it can trigger builds

Editor experience: The dashboard widget gives editors clear feedback about deployment status, which webhooks alone don't provide

My Recommendation

For most production sites, I'd suggest:

  1. Start with GROQ-powered webhooks for automatic deployments of published content (using a GROQ filter to ignore drafts)
  2. Add the Netlify plugin so editors can see deployment status and manually trigger builds when needed

Or use just the dashboard widget if you want full editorial control and to minimize build usage.

The webhook + widget combo gives you the best of both worlds: automatic deploys for published content, plus visibility and manual control when needed. Check out this community answer for a real-world walkthrough of the setup process.

Quick webhook setup example:

  • URL: https://api.netlify.com/build_hooks/YOUR_HOOK_ID
  • Trigger on: Create, Update
  • Filter: !(_id in path("drafts.**")) (ignores drafts)
  • HTTP method: POST

This will trigger a Netlify rebuild whenever you publish content in Sanity!

Hey there User 👋 , seems like what you are looking for would be accomplished via GROQ powered webhooks. There isn't just an out of the box "always redeploy" option on change, but here is the section in the docs on them along with an initial explanation .
To be fair I have limited experience with them so far, but this would definitely be a good chance for you to dive in and learn about them to solve your question
One of my colleagues, User, also shared some more insight with me related to Netlify specific process around this regarding deploys:
In Netlify’s settings are build hooks, which you can create and will get a URL. That URL goes in the URL field of the webhook. They you can set up the webhook for the conditions on which you want to trigger.

Here are some screengrabs to help with the process
user M
Do note though, that this should be done with caution since a redeploy on any content change could be dangerous (not just from a Sanity bandwidth/API call perspective, but also build minutes on Netlify) in that it may unintentionally cause overages etc.
Something worth pointing out and to be mindful
📈 💰
user M

I believe the best solution for this would be this:
https://www.sanity.io/plugins/sanity-plugin-dashboard-widget-netlify
Keep in mind that I haven't tested it myself, but it seems like a less technical method of accomplishing what you need without getting into webhooks to achieve it.
if you were using Vercel instead of Netlify, then this looks like a good option:

https://www.sanity.io/plugins/vercel-deploy
If I recall correctly, the Netlify deploy plugin comes with the pretty big caveat that it exposes an auth token to the Sanity studio client side code since it needs to communicate with either Netlify or Github from the frontend.
Same thing for the Vercel one by the way.
The clean approach is the one recommended by User, using a custom webhook.
Thanks User and User. I will take a look and do some investigating! 🙂
By the way
user Y
and
user F
I had mentioned in my initial post, but there appears to be some plugins that do the trick (like the one below). Do you anything about them?

https://www.sanity.io/plugins/sanity-plugin-dashboard-widget-netlify
As I mentioned, most of the plugins providing a way to trigger deployments directly from the Sanity interface rely on exposing an authorization token to the Sanity studio. Which is technically available to anyone having access to your studio. For both Vercel and Netlify, this token gives full control over your account, so I would personally be worried in exposing it in the browser.
Thanks
user F
. I see. You're right. Thanks for the insight.
I'll look at more closely at the other options. Thanks! :-)
Hi
user F
and
user Y
. Digging in a bit more on this I think Sanity's live Preview may be what's needed and best. However, I have few questions. I'm building a fairly simple website for a client using Sanity and Next.js. I've read through the documentation for setting up Live Preview with Nextjs and Santiy and Sanity's Preview Content. However, for what I'm building -- although there is a blog section -- it is on the most part only pages. And, all of the documentation that I see add code to a [slug].js file. For my purposes my client would like to only preview their pages (not blogs). What would you recommend? Is it better to install the production-preview plugin from Sanity (which seems easier) and manually navigate to the page one wants to preview?
Any insight would be greatly appreciated. thanks!
Hey User. I’m a little confused as what’s blocking you. This
[slug].js
thing is coincidental. This preview system would work on any page. The concept is that your
getStaticProps
functions receive whether your preview mode is enabled, and query Sanity for drafts as well. It doesn’t matter which content types you use that on.
Thanks
user F
. I didn't realize that. I'm familiar with React, but new to nextjs -- this is actually my first time using nextjs -- so that is part of why I'm asking so many questions and I will no doubt have more 🙂.
No worries. 😊 Happy to help.
Hi
user Y
and
user F
. I'm struggling to get a live preview link up and running and I'm going around in circles a bit. The issue I believe is that I'm not able to properly route to the static page to preview as shown in most examples (using getStaticPaths). Silly question perhaps, but can you direct me to examples that show previewing static pages using getStaticProps?
Thanks for your help :-)
It's probably staring me in the face and at this point just can't see it. Thanks again.
Hey User. Here are a couple of links to help you:
Setting up the preview mode on Next.js .
Setting up the preview feature on Sanity .• And then to bridge the two, you can either look at
next-sanity or my own version which is a bit more manual but that I actually prefer other next-sanity.
Essentially it all boils down to this: once you have set up the Next.js preview mode as explained in their documentation, you use that
preview
flag in your
getStaticProps
functions to pick an alternative Sanity client which uses a secret token so it can query drafts. And then there is a little bit of filtering logic (done either by next-sanity or by my version) to pick drafts over published documents.
One main different between next-sanity and my version is that next-sanity ships much significantly more code to the client because it bundles a lot of Sanity utilities. In my case, I ship nothing to the client and keep everything on the server.
Hi User. You offered some help a while back. I thought to reach out with a couple of questions.I currently have a website deployed on Netlify that pulls data from Sanity and uses nextjs to display content in the front-end. We are interested in possibly adding a few animated GIFs.
Two questions:
-- Can I add animated GIFs the same way as I add Images through Sanity?
-- If yes, will the imageUrlBuilder (@sanity/image-url) work the same way?

Love Sanity. Thanks for the help :-)
I’ll answer in help 😉

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?