Corey Ward
Freelance full-stack dev focused on building awesome Jamstack experiences
Drop this into your Studio to let editors know when there's a more recent version of your Studio available, making sure they have the latest fields and validations.
import { useEffect } from "react"
import config from "config:sanity"
const BUNDLE_CHECK_INTERVAL = 60 * 1000
const CHANGES_AVAILABLE_MESSAGE =
"New changes are available! For the best results the page will be refreshed to get the latest updates."
async function getCurrentHash() {
const basePath = (config.project && config.project.basePath) || "/"
const html = await window.fetch(basePath).then((res) => res.text())
const [, hash] = html.match(/app\.bundle\.js\?(\w+)/) || []
return hash
}
let hash = null
let interval = null
const BundleChecker = () => {
useEffect(() => {
getCurrentHash().then((newHash) => {
hash = newHash
})
interval = createInterval()
return () => clearInterval(interval)
}, [])
// We're a react component, in theory, so return null to not render anything
return null
}
export default BundleChecker
const createInterval = () =>
setInterval(async () => {
const newHash = await getCurrentHash()
if (hash && newHash !== hash) {
clearInterval(interval)
if (window.confirm(CHANGES_AVAILABLE_MESSAGE)) {
window.location.reload()
} else {
interval = createInterval()
}
}
}, BUNDLE_CHECK_INTERVAL)
{
"parts": [
{
"implements": "part:@sanity/base/absolutes",
"path": "./bundleChecker.js"
},
]
}
The Sanity Studio runs as a single-page app, so users don't get the latest version every time they change “pages”. Instead, when you deploy changes to the Sanity Studio (e.g. when you run `sanity deploy`) your editors need to refresh their browser or they'll be working on an outdated version that might include different fields, validations, or types, creating inconsistent data that can lead to bugs.
To ameliorate this, drop the contents of `bundleChecker.js` into your Studio and configure it to your `sanity.json` under the `parts` array and your Studio will now make periodic checks (set to once every 60 seconds by default) to see if there are any changes to your Studio code available. If so, it'll prompt the user to refresh.
Freelance full-stack dev focused on building awesome Jamstack experiences
Open-sourceror @ Sanity.io
🚫 MyFile / ✅ MyFile.pdf
Go to Add extensions to asset original filenamesA script to validate that your schema is free of errors and warnings
Go to Validate schema scriptScript to find and delete unused assets in a dataset
Go to Delete unused assetsScript to convert quotation marks in Portable Text blocks
Go to Convert quotation marks for Portable Text