GoogleChrome / workbox

📦 Workbox: JavaScript libraries for Progressive Web Apps
https://developers.google.com/web/tools/workbox/
MIT License
12.3k stars 811 forks source link

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

Open frederikhors opened 1 month ago

frederikhors commented 1 month ago

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:

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:

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:

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?