Search code examples
javascriptregexservice-workerworkbox

Cache non static url with service worker


I have a little problem with the regex in workbox (my service worker). I need to cache a respond of my api and this respond is an image. For that I need to request an url like this: https://myapi/image/12

So I can't inculud a specifique path in my service worker and I need to deal with regex I guess. I tried this:

                   urlPattern: new RegExp('\/image/'),
                handler: 'NetworkFirst',
                options: {
                    networkTimeoutSeconds: 15,
                    cacheName: 'Saved bill img',
                    expiration: {
                      maxEntries: 25,
                      maxAgeSeconds: 60 * 60,
                    },
                }

but It doesn't work, any help ?


Solution

  • The question I should ask was how to define the beginning of a query in a regex. And so their is a lot of answer on that. So this is what I've done:

    new RegExp('^https:\/\/myapi\/image/')