GoogleChrome / GoogleChrome/workbox

How to get a message from workbox-broadcast-update even if there is not update?

Open
#3,349 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
13k
Forks
880
Avg merge
2h 44m
Merged PRs (30d)
8

Description

I'm trying workbox and service-worker for the first time today.

I'm using the `stale-while-revalidate` strategy with `workbox-broadcast-update`'s `BroadcastUpdatePlugin`.

Everything works:

- the browser starts a call

- the Service Worker with Workbox return the cached response and starts a revalidation call

- if the revalidation response is different I get a message! Great!

What I don't know is **how to receive an event even if there is NO UPDATE from the revalidation call**.

This is the code I'm using:

```js
importScripts(
"https://storage.googleapis.com/workbox-cdn/releases/7.1.0/workbox-sw.js"
);

self.skipWaiting();

workbox.core.clientsClaim();

workbox.routing.registerRoute(
({ url }) => url.pathname.startsWith("/images/avatars/"),

new workbox.strategies.StaleWhileRevalidate({
plugins: [new workbox.broadcastUpdate.BroadcastUpdatePlugin()],
})
);
```

I would like something like this:

```js
workbox.routing.registerRoute(
({ url }) => url.pathname.startsWith("/images/avatars/"),

new workbox.strategies.StaleWhileRevalidate({
plugins: [new workbox.broadcastUpdate.BroadcastUpdatePlugin({
callback: (response) => {
if (response.isNew()) {
const message = {};

sendMessageToClient(message);
}
}
})],
})
);
```

Is there a way?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.