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.

StaleWhileRevalidate do not make the second call to update the Service Worker

See original GitHub issue

Library Affected: workbox-webpack-plugin

Browser & Platform: “all browsers”.

Issue or Feature Request Description: I am using Next Offline library for this site. I set this configuration:

workboxOpts: {
                  swDest: '../public/service-worker.js',
                  maximumFileSizeToCacheInBytes: 20000000,
                  runtimeCaching: [
                    {
                      urlPattern: /https:\/\/fonts\.googleapis\.com\/icon[\w\-_\/\.\:\?\=\&\+]*/,
                      handler: 'CacheFirst',
                      options: {
                        cacheName: 'google-fonts',
                        expiration: {
                          maxEntries: 10,
                          maxAgeSeconds: 30 * 24 * 60 * 60, // 1 month
                        },
                      },
                    },
                    {
                      urlPattern: /^((?!tracker).)*\.(jpeg|png|jpg|ico|svg)\??.*/,
                      handler: 'CacheFirst',
                      options: {
                        cacheName: 'img-cache',
                        expiration: {
                          maxEntries: 15,
                          maxAgeSeconds: 2 * 24 * 60 * 60, // 2 days
                        },
                      },
                    },
                    {
                      urlPattern: /https:\/\/embed.widencdn.net\/img\/cityfurniture.*.(jpeg|png|jpg)\?.*/,
                      handler: 'StaleWhileRevalidate',
                    },
                    {
                      urlPattern: /https:\/\/auth-cityfurniture.dotcmscloud.com\/(dA|contentAsset\/image)[\w\-_\/\.\:\?\=\&\+]*/,
                      handler: 'StaleWhileRevalidate',
                    },
                    {
                      urlPattern: /^((?!monetate).)*\.(js)$/,
                      handler: 'StaleWhileRevalidate',
                    },
                  ],
                },

As the js files are configured with the strategy Stale While Revalidate, when I went to the network tab in the Developers tools I was hoping to see two requests for the files cached in the Service Worker (SW), one for the SW request and one for the update of the data on the SW. But I am seeing just one request for the SW JS files. Am I understanding this wrong? It should do another call to revalidate the content of the SW, right?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
jeffposnickcommented, Mar 11, 2021

After investigating a bit more, the reason you’re not seeing revalidation revalidation requests for those URLs is that they’re precached, and therefore the runtime caching strategy doesn’t apply to them. You can verify this by looking at the precache manifest in your service worker:

{
    url: "_next/static/chunks/commons.98875a76810ab7396f70.js",
    revision: "c81835a6270f5f4fdbd3aa185a230a52"
},

Precached URLs will be updated automatically “in the background” when the manifest information for them changes. They don’t need to be revalidated at runtime,

0reactions
davidpaleycommented, Mar 14, 2021

Thank you so much @jeffposnick! You are totally right! And thank you for the blog post, really interesting 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keeping things fresh with stale-while-revalidate - web.dev
stale-while-revalidate helps developers balance between immediacy—loading cached content right away—and freshness—ensuring updates to the cached ...
Read more >
Is there a way to delay cache revalidation in service worker?
So I tried to implement the stale-while-revalidate strategy: fetch the data once, then if the response for a given request is already in...
Read more >
Strategies for service worker caching - Chrome Developers
While the possibilities are practically endless, this guide will stick with the strategies that ship with Workbox, so you can get a sense...
Read more >
Stale-while-revalidate Data Fetching with React Hooks: A Guide
Notice we have added another argument called defaultValue to it. The default value of an API call can be different if you use...
Read more >
Service worker: Caching and offline mode strategies - Apiumhub
For example, if a product is already out of stock, we should not get a cached response such as { stock: 5 }....
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