Skip to content

PrecacheController ​

Class @vite-pwa/workbox-swkit/precaching

Performs efficient precaching of assets.

Constructor ​

ts
new PrecacheController(options?: PrecacheControllerOptions)

Create a new PrecacheController.

Methods ​

activate() ​

ts
activate(event: ExtendableEvent): Promise<CleanupResult>

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.

Returns:

addToCacheList() ​

ts
addToCacheList(entries: string | PrecacheEntry[]): void

This method will add items to the precache list, removing duplicates and ensuring the information is valid.

Parameters

  • entries – Array of entries to precache.

createHandlerBoundToURL() ​

ts
createHandlerBoundToURL(url: string): RouteHandlerCallback

Returns a function that looks up url in the precache (taking into account revision information), and returns the corresponding Response.

Parameters

  • url – The precached URL which will be used to lookup the Response.

Returns:

getCachedURLs() ​

ts
getCachedURLs(): string[]

Returns a list of all the URLs that have been precached by the current service worker.

Returns: The precached URLs.

getCacheKeyForURL() ​

ts
getCacheKeyForURL(url: string): string | undefined

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.

Parameters

  • url – A URL whose cache key you want to look up.

Returns: The versioned URL that corresponds to a cache key for the original URL, or undefined if that URL isn't precached.

getIntegrityForCacheKey() ​

ts
getIntegrityForCacheKey(cacheKey: string): string | undefined

Parameters

  • cacheKey – A cache key whose SRI you want to look up.

Returns: The subresource integrity associated with the cache key, or undefined if it's not set.

getURLsToCacheKeys() ​

ts
getURLsToCacheKeys(): Map<string, string>

Returns a mapping of a precached URL to the corresponding cache key, taking into account the revision information for the URL.

Returns: A URL to cache key mapping.

install() ​

ts
install(event: ExtendableEvent): Promise<InstallResult>

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.

Returns:

matchPrecache() ​

ts
matchPrecache(request: string | Request): Promise<Response | undefined>

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'.

Parameters

  • request – The key (without revisioning parameters) to look up in the precache.

Returns:

precache() ​

ts
precache(entries?: string | PrecacheEntry[]): void

Adds items to the precache list, removing any duplicates and stores the files in the workbox-core.cacheNames|"precache cache" when the service worker installs.

This method can be called multiple times.

Parameters

  • entries – Array of entries to precache.

Released under the MIT License.