Last active
March 16, 2021 21:06
-
-
Save rexxars/ff8ecd6bc111d5d9ca979ecc4b8c1ac9 to your computer and use it in GitHub Desktop.
Sanity studio bundle update checker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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