Code Monkey home page Code Monkey logo

Comments (2)

josephdpurcell avatar josephdpurcell commented on July 29, 2024 3

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.

from self-destroying-sw.

userquin avatar userquin commented on July 29, 2024

@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

from self-destroying-sw.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.