workbox-precaching
Most consumers of this module will want to use the precacheAndRoute() method to add assets to the cache and respond to network requests with these cached assets.
If you require more control over caching and routing, you can use the PrecacheController interface.
Classes
PrecacheController
Performs efficient precaching of assets.
PrecacheFallbackPlugin
PrecacheFallbackPlugin
allows you to specify an "offline fallback"
response to be used when a given strategy is unable to generate a response.
It does this by intercepting the handlerDidError
plugin callback
and returning a precached response, taking the expected revision parameter
into account automatically.
Unless you explicitly pass in a PrecacheController
instance to the
constructor, the default instance will be used. Generally speaking, most
developers will end up using the default.
PrecacheRoute
A subclass of Route that takes a PrecacheController instance and uses it to match incoming requests and handle fetching responses from the precache.
PrecacheStrategy
A Strategy implementation specifically designed to work with PrecacheController to both cache and fetch precached assets.
Note: an instance of this class is created automatically when creating a
PrecacheController
; it's generally not necessary to create this yourself.
Methods
addPlugins
addPlugins(plugins)
Adds plugins to the precaching strategy.
Parameter |
|
---|---|
plugins |
Array of Object |
addRoute
addRoute(options)
Add a fetch
listener to the service worker that will
respond to
network requests
with precached assets.
Requests for assets that aren't precached, the FetchEvent
will not be
responded to, allowing the event to fall through to other fetch
event
listeners.
Parameter |
|
---|---|
options |
Optional Object |
cleanupOutdatedCaches
cleanupOutdatedCaches()
Adds an activate
event listener which will clean up incompatible
precaches that were created by older versions of Workbox.
createHandlerBoundToURL
createHandlerBoundToURL(url, fallbackToNetwork) returns module:workbox-routing~handlerCallback
Helper function that calls PrecacheController#createHandlerBoundToURL on the default PrecacheController instance.
If you are creating your own PrecacheController, then call the PrecacheController#createHandlerBoundToURL on that instance, instead of using this function.
Parameter |
|
---|---|
url |
string The precached URL which will be used to lookup the
|
fallbackToNetwork |
Optional boolean Whether to attempt to get the response from the network if there's a precache miss. |
getCacheKeyForURL
getCacheKeyForURL(url) returns string
Takes in a URL, and returns the corresponding URL that could be used to lookup the entry in the precache.
If a relative URL is provided, the location of the service worker file will be used as the base.
For precached entries without revision information, the cache key will be the same as the original URL.
For precached entries with revision information, the cache key will be the original URL with the addition of a query parameter used for keeping track of the revision info.
Parameter |
|
---|---|
url |
string The URL whose cache key to look up. |
- Returns
-
string
The cache key that corresponds to that URL.
matchPrecache
matchPrecache(request) returns Promise containing (Response or undefined)
Helper function that calls PrecacheController#matchPrecache on the default PrecacheController instance.
If you are creating your own PrecacheController, then call PrecacheController#matchPrecache on that instance, instead of using this function.
Parameter |
|
---|---|
request |
(string or Request) The key (without revisioning parameters) to look up in the precache. |
- Returns
-
Promise containing (Response or undefined)
precache
precache(entries)
Adds items to the precache list, removing any duplicates and stores the files in the "precache cache" when the service worker installs.
This method can be called multiple times.
Please note: This method will not serve any of the cached files for you. It only precaches files. To respond to a network request you call addRoute().
If you have a single array of files to precache, you can just call precacheAndRoute().
Parameter |
|
---|---|
entries |
Optional Array of (Object or string) Array of entries to precache. |
precacheAndRoute
precacheAndRoute(entries, options)
This method will add entries to the precache list and add a route to respond to fetch events.
This is a convenience method that will call precache() and addRoute() in a single call.
Parameter |
|
---|---|
entries |
Array of (Object or string) Array of entries to precache. |
options |
Optional Object |
Abstract types
CleanupResult
Object
Property
Parameter |
|
---|---|
deletedCacheRequests |
Array of string List of URLs that were deleted while cleaning up the cache. |
InstallResult
Object
Properties
Parameter |
|
---|---|
updatedURLs |
Array of string List of URLs that were updated during installation. |
notUpdatedURLs |
Array of string List of URLs that were already up to date. |
PrecacheEntry
Object
Properties
Parameter |
|
---|---|
url |
string URL to precache. |
revision |
Optional string Revision information for the URL. |
integrity |
Optional string Integrity metadata that will be used when making the network request for the URL. |
urlManipulation
urlManipulation(context) returns Array of URL
The "urlManipulation" callback can be used to determine if there are any additional permutations of a URL that should be used to check against the available precached files.
For example, Workbox supports checking for '/index.html' when the URL '/' is provided. This callback allows additional, custom checks.
Parameter |
|||||
---|---|---|---|---|---|
context |
Object Values in
|
- Returns
-
Array of URL
To add additional urls to test, return an Array of URLs. Please note that these should not be strings, but URL objects.