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.

Issue with cacheDidUpdate plugin in workbox-precaching when other callbacks are also used

See original GitHub issue

In my application, I display a splash screen and wait for complete the installation of the service worker and only then display the content, because the service worker must manipulate the csp token for several tabs for all requests in the API.

On a slow connection, the installation of a service worker can take up to a minute. I would like to keep the user busy with the progress of caching elements at this time.

I read #2406 and wrote a custom plugin for cacheDidUpdate:

export const cacheDidUpdate = async ({ cacheName, request, old Response, new Response, event, state }) => {
    const { log } = console;
    log('cacheDidUpdate', cacheName, state);
};

I used it in the service worker

addPlugins([cacheDidUpdate ]);

but I didn’t see anything in the console. What am I doing wrong?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:37 (20 by maintainers)

github_iconTop GitHub Comments

1reaction
jeffposnickcommented, Nov 22, 2021

(I’ve updated the code sample to use event.type === 'install' instead of event instanceof InstallEvent, since the latter apparently won’t work in Safari, where InstallEvent is not defined in the ServiceWorkerGlobalScope.)

1reaction
budarincommented, Oct 6, 2021

Getting back to your specific usage, apart from all other aspects of this discussion, I would recommend that you don’t add any entries to the precache manifest that is generated by the build tools unless you’re sure that the revision field uniquely identifies the actual content associated with the URL. …

This approach greatly complicates the life of the developer and disorients beginners - static resources and a /?source=pwa router make up the precache list for the application to work offline. You suggest dividing this list and processing its elements in different ways.

If I were developing a workbox, I would implement such a contract

new InjectManifest({
    swSrc: '...',
    swDest: 'sw.js',
    include: [
       { 
          url: '/?source=pwa',  // static resource is generated by server
          revision: <revision>,
       },
       { 
          url: '/anotherStaticResourceFromServer',
          revision: <revision>,
       },
       { url: '*.(js|json)' }, 
       { url: '*.css' }, 
    ],
    addIntegrity: true
}),

And under the hood I would get the integrity attribute for static resources and for static resources from server (with revision) I would do nothing - simply put them as is into manifest assets array.

Thus, the developer would only have to focus on working with the rest of the resources that are not included in the precache list.

P.S: I want to emphasize once again that in this case /?source=pwa and /anotherStaticResourceFromServer are static resources that must be only generated by the product server with its environment and cannot be generated during the build process

for example, ‘/?source=pwa’ route - is an app-shell for the pwa, which is rendered by the server at the request of the client from components that are build and tested during the build process

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using plugins - Chrome Developers
This callback is handy if you need to override or normalize URLs prior to using them to access caches. cachedResponseWillBeUsed : This is...
Read more >
workbox changelog
Fixes a bug where the workbox namespace was used before it was defined, ... Plugins implementing the cacheDidUpdate method were not properly bound, ......
Read more >
How do I postMessage with custom plugin (cacheDidUpdate)?
I wrote this code but it do nothing in cacheDidUpdate. ... { PrecacheController } = workbox.precaching let { cacheDidUpdate } = workbox.core ...
Read more >
workbox-webpack-plugin: Versions - Openbase
As part of this change, workbox-build now uses the TypeScript definitions ... In this proposal, all plugin callbacks will also be passed a...
Read more >
Callback plugins - Ansible Documentation
By default, callback plugins control most of the output you see when running the command line programs, but can also be used to...
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