Skip to content

Instantly share code, notes, and snippets.

@rexxars
Last active March 16, 2021 21:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rexxars/ff8ecd6bc111d5d9ca979ecc4b8c1ac9 to your computer and use it in GitHub Desktop.
Save rexxars/ff8ecd6bc111d5d9ca979ecc4b8c1ac9 to your computer and use it in GitHub Desktop.
Sanity studio bundle update checker
import {useEffect} from 'react'
import config from 'config:sanity'
const BUNDLE_CHECK_INTERVAL = 15 * 1000
async function getCurrentHash() {
const basePath = (config.project && config.project.basePath) || '/'
const html = await fetch(basePath).then((res) => res.text())
const [, hash] = html.match(/app\.bundle\.js\?(\w+)/) || []
return hash
}
export default function bundleChecker() {
let hash = null
useEffect(() => {
getCurrentHash().then((newHash) => {
hash = newHash
})
const interval = setInterval(async () => {
const newHash = await getCurrentHash()
if (hash && newHash !== hash) {
window.location.reload()
}
}, BUNDLE_CHECK_INTERVAL)
return () => clearInterval(interval)
}, [])
// We're a react component, in theory, so return null to not render anything
return null
}
{
"root": true,
"//": "...",
"parts": [
{
"implements": "part:@sanity/base/absolutes",
"path": "./bundleChecker.js"
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment