At this point, service workers may seem tricky. There's lots of complex interactions that are hard to get right. Network requests! Caching strategies! Cache management! Precaching! It's a lot to remember. This doesn't make service worker an ill-designed technology; it works as intended, and solves hard problems.
Good abstractions make complex APIs easier to use. That's where Workbox comes in. Workbox is a set of modules that simplify common service worker routing and caching. Each module available addresses a specific aspect of service worker development. Workbox aims to make using service workers as easy as possible, while allowing the flexibility to accommodate complex application requirements where needed.
In the simplest cases,
workbox-build
offers a couple of methods that can generate a service worker that precaches specified assets.
The generateSW
method does most of the work out of the box,
while the injectManifest
method offers more control when necessary.
For more advanced use cases, other modules can help. A few such modules are:
workbox-routing
for request matching.workbox-strategies
for caching strategies.workbox-precaching
for precaching.workbox-expiration
for managing caches.workbox-window
for registering a service worker and handling updates in thewindow context
.
These and other modules help compose service worker code in a declarative fashion that's easier to read and maintain than using service worker APIs directly. This documentation will explain how to use them in an applied fashion.