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

FYI: also available on Vite #10

Open
userquin opened this issue Jul 6, 2022 · 2 comments
Open

FYI: also available on Vite #10

userquin opened this issue Jul 6, 2022 · 2 comments

Comments

@userquin
Copy link

userquin commented Jul 6, 2022

vite-pwa/vite-plugin-pwa#323 (ready to review)

https://deploy-preview-323--vite-plugin-pwa.netlify.app/guide/unregister-service-worker.html

@josephdpurcell
Copy link

First, regarding this ticket: this is good FYI. I assume specifically its this flag: https://vite-pwa-org.netlify.app/guide/unregister-service-worker.html#development.

Second, some back story relevant to this ticket that might be helpful context if anyone lands here.

I ran into a scenario where my domain was overtaken by Vite PWA. I'm new to service workers and for reasons I didn't figure out the website content would not refresh -- Vite PWA was not updating the content. I'm certain this was user error.

My plan was to switch away from Vite PWA (and Vitesse entirely) to hugo. When I did this I found the old site was still loading (I expected this, because at this point I understood that the service worker registers and reads all the things from cache). So, I went down this muddy path of figuring out how to disable the service worker and/or its cache.

Very important: I learned that the service worker js file that was registered (mine was at /sw.js) will automatically update (given certain assumptions). I found these resources helpful:

I resolved on a VERY similar solution to this project:

/sw.js:

self.addEventListener('install', event => {
  self.skipWaiting();
});

self.addEventListener('activate', (event) => {
  navigator.serviceWorker.getRegistrations().then(
    function(registrations) {
      for (let registration of registrations) {
        registration.unregister();
      }
    }
  ); 
});

I took inspiration from this StackOverflow answer: https://stackoverflow.com/a/33705250, though there are important differences.

The problem was that it would throw an exception:

Uncaught TypeError: Cannot read properties of undefined (reading 'getRegistrations')

And, that's when I found this project! @NekR thank you, thank you, thank you.

All that to say, if I had to do this all over again I might have tried the self-destruct on Vite, but that is not a permanent production solution. I truly need to figure out what I had done wrong in my configuration.

Given how "sticky" service workers and Cache API are I suspect this project is very valuable.

@userquin
Copy link
Author

userquin commented Nov 25, 2023

@josephdpurcell I'm including sw cache storage cleanup in vite-pwa-plugin v0.17.1 (I'll try also add the feature for Vite 4 via new v0.16.8 release), you can use my suggestion here #11

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