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

Cache Refresh Issue at a time new Release #28

Open
engr-muzammil opened this issue Sep 4, 2021 · 1 comment
Open

Cache Refresh Issue at a time new Release #28

engr-muzammil opened this issue Sep 4, 2021 · 1 comment

Comments

@engr-muzammil
Copy link

index.js
serviceWorkerRegistration.register();

If I load the application, the first time it cached all data, into the browser cache, but at the time of the new build release, the browser still showing old data which is already cached, I need to perform a hard reload Ctrl + Shift + R.

Any Fix Available except Cache Buster Technique?

@jeffposnick
Copy link
Collaborator

This is due to the newly updated service worker remaining in the waiting stage of the service worker lifecycle by default.

The pwa template offers some hooks to run your own code when a service worker has finished installation and starts waiting:

console.log(
'New content is available and will be used when all ' +
'tabs for this page are closed. See https://cra.link/PWA.'
);
// Execute callback
if (config && config.onUpdate) {
config.onUpdate(registration);
}

Your onUpdate callback function could do whatever you want; it can notify the user about the update and ask them if they want to stay on the old version or reload for the new version, or it could just forcibly perform that upgrade without asking anything.

The following code would do that forced update without asking:

installingWorker.addEventListener("statechange", event => {
  if (event.target.state === "activated") {
    window.location.reload()
  }
});
installingWorker.postMessage({type: 'SKIP_WAITING'});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants