2

I read many tutorials about sw-toolbox and sw-precache. The doubt i am having is :

When reading about sw-precache, i found out most tutorials are using gulp + sw-precache.

Whereas in sw-toolbox case, i saw the example and syntaxes given only for writing code in service-worker.js directly (no gulpfile code).

I am using gulp to automatically generate service-worker file .

So my main concern is to write code for sw-toolbox in gulp file directly so that i need not write sw-toolbox code after generating sw-precache with gulp

Please comment for doubts

Shashank Sood
  • 480
  • 5
  • 16

2 Answers2

4

You no need to write separate code manually for sw-toolbox. You can configure it using runtimeCaching option in sw-precache. As soon as you configured this option, sw-precache will insert sw-toolbox library in serviceWorker and adds different handlers to it. You can find more details from this link.

Hari krishna
  • 156
  • 9
  • so Is runtimeCaching is simlar to sw-toolbox's router.get()? Does it provide all the features like maxage,networktimeoutseconds etc – Shashank Sood Sep 12 '16 at 10:21
-2

When using sw-precache it generates the service worker and you can't edit that file.

When using sw-precache I'd ignore sw-toolbox and just focus on how to configure sw-precache to cache static files and cache runtime files (I.e. cache images or pages as they are requested)

Matt Gaunt
  • 9,434
  • 3
  • 36
  • 57
  • If i want to use sw-toolbox and precache both – Shashank Sood Sep 12 '16 at 10:10
  • You can't so it easily. It's not the way SW-precache was designed to be used. – Matt Gaunt Sep 16 '16 at 18:30
  • see the above answer..And hell yeah, we can obviously edit it after it ahs been generated by the gulp file. – Shashank Sood Sep 19 '16 at 04:57
  • 1
    The entire point of sw-precache is lost if you edit the service worker after it's been generated. The file revisions are set for that instance of files and designed that when you change the files it forces an update, if you manually just change the service worker file and don't update with sw-precache you will end up with some funky problems with users further down the line. – Matt Gaunt Sep 19 '16 at 14:59
  • Okay. It got your point but my doubt is after generating my service worker file with gulp, If I delete my all files(gulp.js,node_modules folder) and leave only service-worker and ofcourse manifest.json; Then also it works normally. And moreover, if i change service worker file manually, it also works as expected(like service worker getting updated in next refresh ). – Shashank Sood Sep 21 '16 at 04:55
  • The caching rules for the service worker generated by precache is designed such that no changes are made to the precached assets unless the filerevisions are updated, which is intended to be done by the gulp build process. So while it might appear "to work and update" I question if you are actually getting fresh content, if your users will get fresh content and how long before you hit a real issue :( – Matt Gaunt Sep 21 '16 at 23:55
  • 1
    See changing anything in the service worker, makes an update to the service worker file. And it gives the user fresh content as and when handled by the user in the activate event. – Shashank Sood Sep 22 '16 at 05:43
  • The fetch even in the sw-precache file ONLY looks in the cache. The service worker file will update, but if the file revisions aren't updated, the install step will not attempt to update the cached files (A feature of sw-precache), meaning that changes to assets aren't cached, meaning the response from the fetch event is the old version of a file from the cache NOT the network. – Matt Gaunt Sep 22 '16 at 16:45