next-pwa icon indicating copy to clipboard operation
next-pwa copied to clipboard

Can't get new service worker to activate using skip waiting

Open DaniAkash opened this issue 4 years ago • 3 comments

Summary

What can I help with? Please keep this clear and concise.

Versions

  • next-pwa: 11.1.0
  • next: 5.3.1

How To Reproduce

Same issue from - https://stackoverflow.com/questions/65537497/cant-get-new-service-worker-to-activate-using-skip-waiting

Expected Behaviors

Skipwaiting should activate the service worker

Additional Context

Next config

const runtimeCaching = require('next-pwa/cache')
const withPWA = require('next-pwa')

const config = withPWA({
  pwa: {
      dest: 'public',
      register: false,
      skipWaiting: false,
      runtimeCaching,
  },
})

DaniAkash avatar Sep 15 '21 12:09 DaniAkash

You are adding wrong event listener to waiting event, that one should used to prompt user, not handle click

shadowwalker avatar Sep 17 '21 04:09 shadowwalker

DaniAkash I think skip waitin installs the new SW, ( it does with us ) but then a reload is needed to activate the new SW.

We are looking to improve our SW lifecycle too, if you want @onair-lena and I could have a chat with you. Share our issues, and our solutions.

sanderkooger avatar Oct 14 '21 11:10 sanderkooger

I have fixed the issue by updating my config to the exact one used in next.js example

@sanderkooger if you want to know the config:

In next.config.js

{
  pwa: {
    dest: 'public',
    runtimeCaching,
  },
}

For updating the service worker: (Mostly it happens automatically but you can have it as a fallback)

window.workbox.addEventListener('waiting', promptNewVersionAvailable)

// in promptNewVersionAvailable

window.workbox.addEventListener('controlling', () => {
   window.location.reload()
})

window.workbox.messageSkipWaiting()

Now I have a completely different issue, I'm removing sentry generated sourcemaps in the final build following the idea from this comment

This also removes the sourcemap of the service worker - this leads to the entire service worker becoming redundant

Full explaination of this issue can be found in the discussion on the sentry repository

DaniAkash avatar Oct 21 '21 15:10 DaniAkash