Yesterday I posted about an update to my Service Worker caching
strategy. If you look at
my ServiceWorker you will see that there is more to it than just
the fix I had to make for storing data in the
I have also introduced a URL routing framework to simplify my logic in the
service worker when dealing with different kinds of requests. For example, I
don’t want to cache requests to Google Analytics or Disquss, and rather than
onfetch handler a lot more complex, it was easier to be declarative
about the routes that I wanted to manage and then control the logic for those
independently from the other routes.
I based the code off of a client-side URL router I made 6 years ago called
LeviRoutes(inspired by the reggae
reggae sauce craze at the time). LeviRoutes followed an
format and for me it worked quite well allowing me to share logic between my
client and server (I still say
IO-Reader was the first true
Progressive Web App six years ago watch below ;)
I morphed the LeviRoutes router only slightly by
making sure that you as the developer can register routes using
router.post for either of those two HTTP request types (it is easy to add more
in) directly inside the Service Worker and I added in functionality to let you
specify what part of the URL you want to match against, for example you could
only have the regex look at the origin part of the URL, or the path, or the
search parameters (query string). I think it is quite flexible.
I will update the LeviRoutes project, but for posterity my current service worker is below and you can see the different types of interceptions I can do.
I encourage everyone to think about their Service Worker
onfetch URL management
in terms of routing like I have done. It makes your life so much easier.
I also encourage you to check out Service Worker toolbox as it stops you from having to invent your own router. I chose to go down this route because at the time Service Worker toolbox wasn’t modular enough for me — I just needed the router — and I like to “NIH to the max” occasionally (if only to flex my brain a little).
It is great to see that Jeff Posnick is working on making Service Worker Toolbox more modular and that will allow me just to use the route or just the caching utils.
Watch Jeff and his plans for sw-toolbox (I recommend it because you can see the benefits of not going down the path that I chose :)