question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add message event listener when skipWaiting: false

See original GitHub issue

What problem does this feature solve?

When developing PWA, the default workboxPluginMode is generateSW, and they set skipWaiting: false. In this case, the generated service worker should allow our web app to trigger skipWaiting() on demand, by using postMessage() to communicate with our service worker.

This feature was implemented in Workbox v4.1.0, but Vue CLI 3.x can’t take advantage of it.

What does the proposed API look like?

self.addEventListener('message', (event) => {
  if (event.data && event.data.type === 'SKIP_WAITING') {
    self.skipWaiting();
  }
});

Reference: skipWaiting option in workbox-webpack-plugin

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:24 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
wizardpiscescommented, Jun 9, 2019

@LinusBorg @loliconer @wizardpisces

I am now using the v4 alpha which comes with Workbox 4

I have tried adding this navigator.serviceWorker.controller.postMessage({ type: 'SKIP_WAITING'}); inside the updated() hook inside register-service-worker.js

But this gives me the following error:

Error during service worker registration: TypeError: Cannot read property 'postMessage' of null

I have also tried adding

updated(reg) {
    window.addEventListener('refreshSW', function () {
      reg.waiting.postMessage({ type: 'SKIP_WAITING' })
    })
}
`
``

but this does nothing

I would really really like to know how to immediately clear the cache when new content is detected. I am so boggled that this is so damn hard to accomplish and how is this not affecting thousands of vue-cli developers? Everytime I push a new breaking change it seems like all my users run into issues with the cache not being cleared

this article might help you

1reaction
loliconercommented, Mar 25, 2019

@LinusBorg Yes, I can send a message to the SW, my code in registerServiceWorker.js:

    updated (reg) {
      console.log('New content is available; please refresh.')
      window.addEventListener('refreshSW', function () {
        reg.waiting.postMessage({ type: 'SKIP_WAITING' })
      })
    },

The problem is the ServiceWorker can’t skip waiting after posting message, because it hasn’t a listener to listen the message event. So the code:

self.addEventListener('message', (event) => {
  if (event.data && event.data.type === 'SKIP_WAITING') {
    self.skipWaiting();
  }
});

should be included in the service-worker.js, which is auto-generated. But now the code doesn’t exist in service-worker.js.

Read more comments on GitHub >

github_iconTop Results From Across the Web

active serviceWorker is not sending message to waiting ...
There a few things wrong. navigator.serviceWorker.controller.postMessage('skipWaiting');. This is sending a message from window to worker ...
Read more >
ServiceWorkerGlobalScope.skipWaiting() - Web APIs | MDN
skipWaiting () from inside of an InstallEvent handler. The following example causes a newly installed service worker to progress into the ...
Read more >
Handling Service Worker updates in your Vue PWA
Next up is listening for this event from within our Vue app. ... updateExists = false // Make sure we only send a...
Read more >
How to Fix the Refresh Button When Using Service Workers
We start by adding code to the Service Worker's script to listen for a posted message: addEventListener('message', messageEvent => {
Read more >
Service Workers - W3C
A service worker has an associated skip waiting flag . ... The following is the event handler (and its corresponding event handler event ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found