Use our service worker libraries to simplify your development by eliminating service worker boilerplate code.
sw-precache—Integrates with your build process to generate a service worker that precaches static assets such as, for example, an Application Shell.
sw-toolbox—Implement common runtime caching patterns such as dynamic content, API calls and third-party resources as easily as writing a README.
sw-offline-google-analytics—Temporarily hold and retry analytics requests to avoid losing them to network disconnections.
Why use service worker libraries?
You're sold on the advantages of adding a service worker to your web app—swapping the uncertainty of the network for the promise of a fast, offline- first, service worker-powered experience. But to write your own service worker from scratch, you have to clear some hurdles:
- Precaching URLs easily and reliably.
- Incrementing a cache version string to ensure that precached resources are updated.
- Implementing a cache expiration strategy to account for cache size or entry age.
- Building common patterns such as lie-fi network timeouts and boilerplate code.
- Capturing and reporting Google analytics data during offline usage.
You can address all of these drawbacks using our service worker libraries.
Service Worker Precache
Service Worker Precache (
sw-precache) is a
module for generating a service worker that
like those written with
gulp, and it also provides a
command-line interface. You can use the module
directly, or if you'd prefer, use of the wrappers
sw-precache for specific build environments, like
|Build-time Integration||Drop it into your existing build process: Gulp, Grunt, or command line.|
|Stay Fresh||Changes in your build update the service worker script. Users get updates, but you don't have to manually version your content or caches.|
|No Network, No Problem||Your static resources are served cache-first, quickly, whether or not there's a network available.|
Service Worker Toolbox
Service Worker Toolbox (
some simple helpers for use in creating your own service workers. Specifically,
it provides common caching patterns and an
to using those strategies for runtime requests.
|Runtime Caching||Cache large or infrequently used resources, like images, at runtime, when they're first used.|
|Offline Fallbacks||Load fresh images, API responses, or other dynamic content from the network while online, but fall back to a cached placeholder while offline.|
|Goodbye Lie-Fi||Fight lie-fi by automatically falling back to a cached response when the network is too slow.|
|Battle Cache Bloat||That image from last month doesn't need to be cached forever. Least-recently used and age-based cache expiration helps free up space.|
Offline Google Analytics
Offline Google Analytics temporarily holds and retries analytics requests to avoid losing them to network disconnections. This tool easily installs to your build system using npm and is easily imported into your service worker script. Configure it using a parameterized function call.
|Temporarily Caches Data||Holds analytics requests that are made when the device is offline and retries them the next time the service worker starts up.|
|Custom Replay Values||Key/value pairs to be added to replayed Google Analytics requests. For example, you might set a custom dimension to indicate that a request was replayed.|
|Modified Hit Parameters||Lets you programmatically modify a hit's parameters to, for example, track the elapsed time between when a hit is attempted and when it is replayed.|
Getting started with sw-toolbox by Dean Hume
Adding offline support to create-react-app using sw-precache by Jeffrey Posnick
Jeff Posnick's presentation from the Chrome Dev Summit 2015,
Instant Loading with Service Workers, describes how to effectively use
sw-toolbox to build web apps that load quickly and
Matt Gaunt and Addy Osmani explain how our service worker libraries can help
your web apps work offline in next to no time. This video describes both
In this episode of Totally Tooling Mini-Tips, Matt and Addy step through
From Google I/O 2016, Mat Scales describes great libraries and tools for making progressive web apps load fast, work great offline, and enhace progressively, all for a better user experience.