user5242820
user5242820

Reputation:

Vue PWA not getting new content after refresh

I'm new to Vue and created a project with the PWA Service-worker plugin. After deploying a new version of my App I get these messages in console:

Failure

After refreshing the page (F5) these messages still appear the same way and the App is still in it's old state. I tried everything to clear the cache but it still won't load the new content.

I haven't changed anything from the default config after creating my project and didn't add any code which interacts with the serviceworker. What is going wrong? Am I missing something?

Upvotes: 50

Views: 28302

Answers (4)

vanngoh
vanngoh

Reputation: 658

For those who's struggling with the same problem, here's the latest solution in 2024 which works well for me.

// vue.config.js
...
    pwa: {
        workboxOptions: {
            skipWaiting: true, // force to activate the latest service worker
            clientsClaim: true // force auto reload when service worker is activated
        }
    }
...

In case you're using vite for your PWA, this might be helpful for your configuration as well.

// vite.config.js

VitePWA({
  registerType: 'autoUpdate'
})

Hope this helps!

Upvotes: 0

M. Emre Yalçın
M. Emre Yalçın

Reputation: 654

pass registration argument then use the update() with that. the argument uses ServiceWorkerRegistration API

updated (registration) {
  console.log('New content is available; please refresh.')
  registration.update()
},

Upvotes: 1

Aaron Angle
Aaron Angle

Reputation: 121

I figured out a different approach to this and from what I've seen so far it works fine.

updated() {
      console.log('New content is available; please refresh.');
      caches.keys().then(function(names) {
        for (let name of names) caches.delete(name);
      });
    },

What's happening here is that when the updated function gets called in the service worker it goes through and deletes all the caches. This means that your app will start up slower if there is an update but if not then it will serve the cached assets. I like this approach better because service workers can be complicated to understand and from what I've read using skipWaiting() isn't recommend unless you know what it does and the side effects it has. This also works with injectManifest mode which is how I'm currently using it.

Upvotes: 7

user5242820
user5242820

Reputation:

As I figured out, this question is really only related to beginners in PWA, which don't know that you can (and need) to configure PWA for achieving this. If you feel addressed now (and using VueJS) remember:

To automatically download the new content, you need to configure PWA. In my case (VueJS) this is done by creating a file vue.config.js in the root directory of my project (On the same level as package.json).

Inside this file you need this:

module.exports = {
    pwa: {
        workboxOptions: {
            skipWaiting: true
        }
    }
}

Which will automatically download your new content if detected. However, the content won't be displayed to your client yet, since it needs to refresh after downloading the content. I did this by adding window.location.reload(true) to registerServiceWorker.js in my src/ directory:

updated () {
    console.log('New content is available: Please refresh.')
    window.location.reload(true)
},

Now, if the Service Worker detects new content, it will download it automatically and refresh the page afterwards.

Upvotes: 69

Related Questions