
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeThis is a classic Gatsby + service worker caching issue! When your client updates content in Sanity, they're seeing stale content because of how static site generators and service workers work. Let me break down what's happening and how to fix it.
You're dealing with two layers of caching:
gatsby-plugin-offline, it aggressively caches pages for offline functionality, which means visitors see the cached version until they manually refreshThe content appearing after a hard refresh is the telltale sign of service worker caching issues.
You need to fix this from both angles:
The most reliable approach is setting up a webhook from Sanity to trigger Netlify rebuilds automatically when content changes.
Step 1: Create a Netlify Build Hook
In your Netlify dashboard:
Step 2: Set Up a Sanity Webhook
In your Sanity project dashboard (manage.sanity.io):
production)* to trigger on all document changes, or be more specific like _type == "post" || _type == "page"Now whenever your client publishes content changes in Sanity, Netlify will automatically rebuild the site.
If you're using gatsby-plugin-offline, add this to your gatsby-browser.js file:
export const onServiceWorkerUpdateReady = () => {
window.location.reload()
}This automatically reloads the page when a new service worker (with fresh content from your rebuild) is ready, ensuring visitors see the updated content without needing to manually refresh.
Alternative: Show a notification instead of auto-reloading:
export const onServiceWorkerUpdateReady = () => {
const answer = window.confirm(
`This site has been updated. Reload to display the latest version?`
)
if (answer === true) {
window.location.reload()
}
}Many developers find that gatsby-plugin-offline causes more headaches than it's worth. If offline functionality isn't critical for your use case, consider removing it entirely from your gatsby-config.js. Modern browsers provide reasonable caching without it, and you'll avoid these stale content issues altogether.
To remove it, simply delete it from your plugins array and run npm uninstall gatsby-plugin-offline.
Avoid Build Spam: If your client makes lots of rapid edits, each one will trigger a new build. Consider:
Check Cache Headers: Look at your netlify.toml file and make sure you're not setting overly aggressive Cache-Control headers that could compound the caching issues.
Build Times: Gatsby rebuilds can take several minutes depending on your site size. Set expectations with your client that there will be a short delay (typically 2-5 minutes) between publishing and seeing changes live.
Alternative Approach: If you need more real-time updates without waiting for rebuilds, you might want to explore Sanity's Live Content API with frameworks that support it, though this would require moving away from Gatsby's static generation approach.
The combination of webhook-triggered rebuilds + proper service worker update handling with onServiceWorkerUpdateReady should completely solve your stale content problem!
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store