Place /.* route at the bottom of runtimeCaching

harish0507
0
harish0507
commented 7 months ago

The Problem

I'm trying to add networkOnly strategy for my API routes with the below configuration

runtimeCaching: [{
    urlPattern: '/api/.*',
    handler: 'networkOnly',
    strategyOptions: {
      fetchOptions: {
        mode: 'same-origin',
        credentials: 'same-origin'
      }
    }
}]

But the order in generated sw.js was,

...
workbox.routing.registerRoute(new RegExp('/.*'), workbox.strategies.networkFirst({}), 'GET')
workbox.routing.registerRoute(new RegExp('/api/.*'), workbox.strategies.networkOnly({"fetchOptions":{"mode":"same-origin","credentials":"same-origin"}}), 'GET')
...

Since /.*/ is above the api route configuration /api/* routes uses networkFirst strategy

Suggested Fix

Change the order while concating _runtimeCaching and runtimeCaching options in addTemplates method

...
runtimeCaching: [].concat(options._runtimeCaching, options.runtimeCaching)
.map(i => (Object.assign({}, i, {
  urlPattern: i.urlPattern,
  handler: i.handler || 'networkFirst',
  method: i.method || 'GET'
}))),
...

To

...
runtimeCaching: [].concat(options.runtimeCaching, options._runtimeCaching)
.map(i => (Object.assign({}, i, {
  urlPattern: i.urlPattern,
  handler: i.handler || 'networkFirst',
  method: i.method || 'GET'
}))),
...
0
pi0
31.7k
pi0
commented 7 months ago

@harish0507 Would you please try 3.0.0-beta.0?

0
harish0507
0
harish0507
commented 7 months ago

@pi0 Thanks! 3.0.0-beta.0 fixed this issue.

0
Informations
Question โ€ข Unresolved
#c93 - Created 7 months ago