cyyyu / parcel-plugin-sw-precache

A Parcel plugin for generating a service worker that precaches resources.
MIT License
73 stars 9 forks source link

sw-precache runtimeCaching seems to be ignored #22

Open WayneHiller opened 5 years ago

WayneHiller commented 5 years ago

I want some URLs to always be supplied from the server. Am I doing this correctly? Not sure if the /.../ will be supplied as a regex?

  "sw-precache": {
    "maximumFileSizeToCacheInBytes": 10485760,
    "runtimeCaching": [
      { 
        "urlPattern": "/getincidentattachmentdata/",
        "handler": "networkOnly"
      }
    ]
  }

This is at the end of the serviceworker.js file. Should that be a string or regex?

}), toolbox.router.get("/getincidentattachmentdata/", toolbox.networkOnly, {});
cyyyu commented 5 years ago

right, that should be a regex. pr is welcome.

it's line 44 we convert those strings to regexps. since runtimeCaching is an array, you may need to loop deeper. https://github.com/cyyyu/parcel-plugin-sw-precache/blob/master/index.js#L44

WayneHiller commented 5 years ago

Actually I did a bunch of digging through the docs and it seems a string should work as well.

urlPattern - an Express style route. See the docs for the path-to-regexp module for the full syntax

It seems they use https://github.com/pillarjs/path-to-regexp under the hood.

In my case it was actually better to just set navigateFallback to blank:

 "sw-precache": {
    "maximumFileSizeToCacheInBytes": 10485760,
    "navigateFallback": ""
  }

It would probably make more sense to just use a regexp instead of those Express style paths.

cyyyu commented 5 years ago

Nice investigation. I personally prefer Express style pattern but I would say they are very similar and both easily defined in most cases. Feel free to file a pr if you still want regexp support in this plugin.

WayneHiller commented 5 years ago

I was reading through the sw-precache to workbox conversion notes at https://developers.google.com/web/tools/workbox/guides/migrations/migrate-from-sw and it states:

Deprecated options Express-style wildcard routes are no longer supported. If you were using Express-style wildcard routes in either the runtimeCaching configuration or directly in sw-toolbox, please migrate to an equivalent regular expression route when using Workbox.

So maybe it would be best to change it to use a RegExp.

nickimola commented 5 years ago

Any news on this? I need to do the same but I don't quite understand how I need to write the urlPattern for this to work...

cyyyu commented 5 years ago

@nickimola As said you could use Express-style wildcard routes which are presented in strings rather than regular expressions. Check out the doc of sw-precache. Express-style wildcard routes is deprecated in sw-toolbox but this plugin actually relies on sw-precache. And don't worry about backward compatibility for now as I will bump a major version if we eventually migrate to sw-toolbox.