'How do I postMessage with custom plugin (cacheDidUpdate)?
I want to count every updated or installed cache and send this value to client. How can I do this? I wrote this code but it do nothing in cacheDidUpdate. What is the easiest way to do it? My sw.js:
importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js')
let { PrecacheController } = workbox.precaching
let { cacheDidUpdate } = workbox.core
async function notifyClients(message) {
let clients = await self.clients.matchAll({ includeUncontrolled: true });
clients.forEach(client => client.postMessage(message))
}
notifyClients({ type: 'INSTALLING' })
let cached = 0;
let manifest = self.__WB_MANIFEST;
const precacheController = new PrecacheController(
{},
'precache',
[
{
cacheDidUpdate: async (e) => {
console.log('CACHE_DID_UPDATE');
notifyClients({
type: 'CACHE_DID_UPDATE',
cached: ++cached,
total: manifest.length
})
}
}
]
)
precacheController.addToCacheList(manifest)
self.addEventListener('install', async (event) => {
await event.waitUntil(precacheController.install(event));
notifyClients({ type: 'DONE' })
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
