Skip to content
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

Closed
soc221b opened this issue Jan 7, 2023 · 3 comments
Closed

Invalidate cache for service worker / PWA #267

soc221b opened this issue Jan 7, 2023 · 3 comments
Labels
enhancement New feature or request stale

Comments

@soc221b
Copy link
Member

soc221b commented Jan 7, 2023

No description provided.

@soc221b soc221b added the enhancement New feature or request label Jan 7, 2023
@soc221b
Copy link
Member Author

soc221b commented Jan 8, 2023

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()

@stale
Copy link

stale bot commented Feb 8, 2023

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.

@stale stale bot added the stale label Feb 8, 2023
@soc221b soc221b removed the stale label Feb 8, 2023
@soc221b soc221b pinned this issue Feb 22, 2023
@stale
Copy link

stale bot commented Mar 10, 2023

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.

@stale stale bot added the stale label Mar 10, 2023
@stale stale bot closed this as completed Mar 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request stale
Projects
None yet
Development

No branches or pull requests

1 participant