workbox-precaching
Summary
- Types
- Methods
Types
CleanupResult
Properties
- deletedCacheRequestsstring[] 
InstallResult
Properties
- notUpdatedURLsstring[] 
- updatedURLsstring[] 
PrecacheController
Performs efficient precaching of assets.
Properties
- constructorfunction Create a new PrecacheController. The constructorfunction looks like:(options?: PrecacheControllerOptions) => {...}- optionsPrecacheControllerOptions optional 
 - returns
 
- strategyStrategy 
- activatefunction Deletes assets that are no longer present in the current precache manifest. Call this method from the service worker activate event. Note: this method calls event.waitUntil()for you, so you do not need to call it yourself in your event handlers.The activatefunction looks like:(event: ExtendableEvent) => {...}- eventExtendableEvent 
 - returnsPromise<CleanupResult> 
 
- addToCacheListfunction This method will add items to the precache list, removing duplicates and ensuring the information is valid. The addToCacheListfunction looks like:(entries: (string | PrecacheEntry)[]) => {...}- entries(string | PrecacheEntry)[] Array of entries to precache. 
 
- createHandlerBoundToURLfunction Returns a function that looks up urlin the precache (taking into account revision information), and returns the correspondingResponse.The createHandlerBoundToURLfunction looks like:(url: string) => {...}- urlstring The precached URL which will be used to lookup the Response.
 - returns
 
- getCacheKeyForURLfunction Returns the cache key used for storing a given URL. If that URL is unversioned, like `/index.html', then the cache key will be the original URL with a search parameter appended to it. The getCacheKeyForURLfunction looks like:(url: string) => {...}- urlstring A URL whose cache key you want to look up. 
 - returnsstring The versioned URL that corresponds to a cache key for the original URL, or undefined if that URL isn't precached. 
 
- getCachedURLsfunction Returns a list of all the URLs that have been precached by the current service worker. The getCachedURLsfunction looks like:() => {...}- returnsstring[] The precached URLs. 
 
- getIntegrityForCacheKeyfunction The getIntegrityForCacheKeyfunction looks like:(cacheKey: string) => {...}- cacheKeystring 
 - returnsstring The subresource integrity associated with the cache key, or undefined if it's not set. 
 
- getURLsToCacheKeysfunction Returns a mapping of a precached URL to the corresponding cache key, taking into account the revision information for the URL. The getURLsToCacheKeysfunction looks like:() => {...}- returnsMap<stringstring> A URL to cache key mapping. 
 
- installfunction Precaches new and updated assets. Call this method from the service worker install event. Note: this method calls event.waitUntil()for you, so you do not need to call it yourself in your event handlers.The installfunction looks like:(event: ExtendableEvent) => {...}- eventExtendableEvent 
 - returnsPromise<InstallResult> 
 
- matchPrecachefunction This acts as a drop-in replacement for cache.match()with the following differences:- It knows what the name of the precache is, and only checks in that cache.
- It allows you to pass in an "original" URL without versioning parameters, and it will automatically look up the correct cache key for the currently active revision of that URL.
 E.g., matchPrecache('index.html')will find the correct precached response for the currently active service worker, even if the actual cache key is'/index.html?__WB_REVISION__=1234abcd'.The matchPrecachefunction looks like:(request: string | Request) => {...}- requeststring | Request The key (without revisioning parameters) to look up in the precache. 
 - returnsPromise<Response> 
 
- precachefunction Adds items to the precache list, removing any duplicates and stores the files in the cache" when the service worker installs. This method can be called multiple times. The precachefunction looks like:(entries: (string | PrecacheEntry)[]) => {...}- entries(string | PrecacheEntry)[] 
 
PrecacheEntry
Properties
- integritystring optional 
- revisionstring optional 
- urlstring 
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.
Properties
- constructorfunction Constructs a new PrecacheFallbackPlugin with the associated fallbackURL. The constructorfunction looks like:(config: object) => {...}- configobject - fallbackURLstring A precached URL to use as the fallback if the associated strategy can't generate a response. 
- precacheControllerPrecacheController optional 
 
 - returns
 
PrecacheRoute
A subclass of workbox-routing.Route that takes a workbox-precaching.PrecacheController instance and uses it to match incoming requests and handle fetching responses from the precache.
Properties
- constructorfunction The constructorfunction looks like:(precacheController: PrecacheController, options?: PrecacheRouteOptions) => {...}- precacheControllerA PrecacheControllerinstance used to both match requests and respond to fetch events.
- optionsPrecacheRouteOptions optional 
 - returns
 
- catchHandlerRouteHandlerObject optional 
- handler
- match
- methodHTTPMethod 
- setCatchHandlerfunction The setCatchHandlerfunction looks like:(handler: RouteHandler) => {...}- handlerA callback function that returns a Promise resolving to a Response 
 
PrecacheRouteOptions
Properties
- cleanURLsboolean optional 
- directoryIndexstring optional 
- ignoreURLParametersMatchingRegExp[] optional 
- urlManipulationurlManipulation optional 
PrecacheStrategy
A workbox-strategies.Strategy implementation specifically designed to work with workbox-precaching.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.
Properties
- constructorfunction The constructorfunction looks like:(options?: PrecacheStrategyOptions) => {...}- optionsPrecacheStrategyOptions optional 
 - returns
 
- cacheNamestring 
- fetchOptionsRequestInit optional 
- matchOptionsCacheQueryOptions optional 
- plugins
- copyRedirectedCacheableResponsesPlugin
- defaultPrecacheCacheabilityPlugin
- _awaitCompletefunction The _awaitCompletefunction looks like:(responseDone: Promise<Response>, handler: StrategyHandler, request: Request, event: ExtendableEvent) => {...}- responseDonePromise<Response> 
- handler
- requestRequest 
- eventExtendableEvent 
 - returnsPromise<void> 
 
- _getResponsefunction The _getResponsefunction looks like:(handler: StrategyHandler, request: Request, event: ExtendableEvent) => {...}- handler
- requestRequest 
- eventExtendableEvent 
 - returnsPromise<Response> 
 
- _handleFetchfunction The _handleFetchfunction looks like:(request: Request, handler: StrategyHandler) => {...}- requestRequest 
- handler
 - returnsPromise<Response> 
 
- _handleInstallfunction The _handleInstallfunction looks like:(request: Request, handler: StrategyHandler) => {...}- requestRequest 
- handler
 - returnsPromise<Response> 
 
- handlefunction Perform a request strategy and returns a Promisethat will resolve with aResponse, invoking all relevant plugin callbacks.When a strategy instance is registered with a Workbox workbox-routing.Route, this method is automatically called when the route matches.Alternatively, this method can be used in a standalone FetchEventlistener by passing it toevent.respondWith().The handlefunction looks like:(options: FetchEvent | HandlerCallbackOptions) => {...}- optionsFetchEvent | HandlerCallbackOptions A FetchEventor an object with the properties listed below.
 - returnsPromise<Response> 
 
- handleAllfunction Similar to workbox-strategies.Strategy~handle, but instead of just returning aPromisethat resolves to aResponseit it will return an tuple of[response, done]promises, where the former (response) is equivalent to whathandle()returns, and the latter is a Promise that will resolve once any promises that were added toevent.waitUntil()as part of performing the strategy have completed.You can await the donepromise to ensure any extra work performed by the strategy (usually caching responses) completes successfully.The handleAllfunction looks like:(options: FetchEvent | HandlerCallbackOptions) => {...}- optionsFetchEvent | HandlerCallbackOptions A FetchEventor an object with the properties listed below.
 - returns[Promise<Response>, Promise<void>] A tuple of [response, done] promises that can be used to determine when the response resolves as well as when the handler has completed all its work. 
 
urlManipulation
          
          
            workbox-precaching.urlManipulation(
        { url }:
  object,
      
    )
          
        Type
function
Parameters
- { url }object - urlURL 
 
Returns
- URL[] 
Methods
addPlugins
          
          
            workbox-precaching.addPlugins(
        plugins:
  WorkboxPlugin[],
      
    )
          
        Adds plugins to the precaching strategy.
Parameters
- plugins
addRoute
          
          
            workbox-precaching.addRoute(
        options?:
  PrecacheRouteOptions,
      
    )
          
        Add a fetch listener to the service worker that will respond to [network requests]https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers#Custom_responses_to_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.
Parameters
- optionsPrecacheRouteOptions optional 
cleanupOutdatedCaches
          
          
            workbox-precaching.cleanupOutdatedCaches()
          
        Adds an activate event listener which will clean up incompatible precaches that were created by older versions of Workbox.
createHandlerBoundToURL
          
          
            workbox-precaching.createHandlerBoundToURL(
        url:
  string,
      
    )
          
        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.
Parameters
- urlstring The precached URL which will be used to lookup the Response.
Returns
getCacheKeyForURL
          
          
            workbox-precaching.getCacheKeyForURL(
        url:
  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.
Parameters
- urlstring The URL whose cache key to look up. 
Returns
- string | undefined - The cache key that corresponds to that URL. 
matchPrecache
          
          
            workbox-precaching.matchPrecache(
        request:
  string | Request,
      
    )
          
        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.
Parameters
- requeststring | Request The key (without revisioning parameters) to look up in the precache. 
Returns
- Promise<Response | undefined> 
precache
          
          
            workbox-precaching.precache(
        entries:
  (string | PrecacheEntry)[],
      
    )
          
        Adds items to the precache list, removing any duplicates and stores the files in the 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 workbox-precaching.addRoute.
If you have a single array of files to precache, you can just call workbox-precaching.precacheAndRoute.
Parameters
- entries(string | PrecacheEntry)[] 
precacheAndRoute
          
          
            workbox-precaching.precacheAndRoute(
        entries:
  (string | PrecacheEntry)[],
        options?:
  PrecacheRouteOptions,
      
    )
          
        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 workbox-precaching.precache and workbox-precaching.addRoute in a single call.
Parameters
- entries(string | PrecacheEntry)[] Array of entries to precache. 
- optionsPrecacheRouteOptions optional