-
-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Invalidate cache for service worker / PWA #267
Labels
Comments
If you are using workbox@6, here is a workaround: // patch-service-worker.js
const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
const indexHtmlPath = path.resolve('.', 'dist', 'index.html')
const serviceWorkerPath = path.resolve('.', 'dist', 'service-worker.js')
/**
* After running `import-meta-env` to inject environment variables,
* run this script to patch revision of index.html.
*/
const main = () => {
const newIndexHtmlRevision = getFileHash(indexHtmlPath)
patchIndexHtmlRevision(newIndexHtmlRevision)
}
/**
* https://github.com/GoogleChrome/workbox/blob/v6.5.4/packages/workbox-build/src/lib/get-file-hash.ts
* @param {string} file
* @returns {string}
*/
const getFileHash = file => {
try {
const buffer = fs.readFileSync(file)
return getStringHash(buffer)
} catch (err) {
console.debug(err)
}
}
/**
* https://github.com/GoogleChrome/workbox/blob/v6.5.4/packages/workbox-build/src/lib/get-string-hash.ts
* @param {Buffer} input
* @returns {string}
*/
const getStringHash = input => {
const md5 = crypto.createHash('md5')
md5.update(input)
return md5.digest('hex')
}
/**
* @param {string} revision
*/
const patchIndexHtmlRevision = revision => {
const serviceWorkerContent = fs.readFileSync(serviceWorkerPath, 'utf8')
const hashRegex = /\{url:"\/index\.html",revision:"[0-9a-f]{32}"\}/
if (hashRegex.test(serviceWorkerContent) === false) {
throw Error('[patch-service-worker] failed to match.')
}
const newServiceWorkerContent = serviceWorkerContent.replace(
hashRegex,
`{url:"/index.html",revision:"${revision}"}`,
)
fs.writeFileSync(serviceWorkerPath, newServiceWorkerContent, 'utf8')
}
main() |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: