Vitaliy
Vitaliy

Reputation: 429

sw-precache-config, ignore path does not work?

I have following sw-precache-config.js

module.exports = {
  staticFileGlobs: [
    'index.html',
    'manifest.json',
    'bower_components/webcomponentsjs/*',
  ],
  navigateFallback: 'index.html',
  runtimeCaching: [{
    urlPattern: /.+\/api\/.+/,
    handler: 'networkOnly',
  }],
};

I have SPA application with service-worker (generated using above mentioned config) to cache resources.

When I open myapp/api/route without cached resources (first visit of website), it works fine.

If I open myapp's main page, browser caches resources and subsequent requests to myapp/api/route cause problems (layout of myapp's main page is generated but without data).

When I clear browsers' cache (Chrome - Developer Tools - tab Application - Cache storage - Clear) I can open myapp/api/route again.

So my problem is: how can I force browser to ignore service-worker.js for all "myapp/api/*" routes?

Upvotes: 0

Views: 358

Answers (1)

Lewis
Lewis

Reputation: 14866

You just need to simply ignore requests containing the pattern.

// service-worker.js   
self.onfetch = evt => {
  let path = new URL(evt.request.url).pathname;
  if(path.startsWith('/myapp/api')) {
    // these requests will be networked
    return;
  }
  // Handle other requests here
};

Upvotes: 1

Related Questions