NGriffin
NGriffin

Reputation: 81

Caching all query string results from a domain with Workbox

I am attempting to move from sw-toolbox to Workbox and one of the things I need to do is cache all query string results from an API url that is on a different server, I tried out some code, but I haven't had any successes yet.

Here's my latest attempt:

workbox.routing.registerRoute(
  'https://domain.example-third-party.co.uk/API/' + '(.*)',
  workbox.strategies.cacheFirst({
    cacheName: 'extra',
    plugins: [
      new workbox.cacheableResponse.Plugin({
        statuses: [0, 200]
      })
    ]
  })
);

And I've tried without the '(.*)' by the way.

Upvotes: 1

Views: 744

Answers (1)

Jeff Posnick
Jeff Posnick

Reputation: 56064

In Workbox v3 (which looks like what you're using, based on plugins syntax), you'd want to pass in a RegExp as the criteria to match the route.

workbox.routing.registerRoute(
  new RegExp('^https://domain\.example-third-party\.co\.uk/API/'),
  workbox.strategies.cacheFirst({
    cacheName: 'extra',
    plugins: [
      new workbox.cacheableResponse.Plugin({
        statuses: [0, 200]
      })
    ]
  })
);

There's an example at https://developers.google.com/web/tools/workbox/guides/migrations/migrate-from-sw#migrate_from_hand-crafted_sw-toolbox_to_workbox-sw which you might have seen already, and a general guide to routing in Workbox v3 at https://developers.google.com/web/tools/workbox/guides/route-requests

Upvotes: 1

Related Questions