UNPKG

5.82 kBTypeScriptView Raw
1import { Strategy } from 'workbox-strategies/Strategy.js';
2import { RouteHandlerCallback, WorkboxPlugin } from 'workbox-core/types.js';
3import { PrecacheEntry, InstallResult, CleanupResult } from './_types.js';
4import './_version.js';
5declare global {
6 interface ServiceWorkerGlobalScope {
7 __WB_MANIFEST: Array<PrecacheEntry | string>;
8 }
9}
10interface PrecacheControllerOptions {
11 cacheName?: string;
12 plugins?: WorkboxPlugin[];
13 fallbackToNetwork?: boolean;
14}
15/**
16 * Performs efficient precaching of assets.
17 *
18 * @memberof workbox-precaching
19 */
20declare class PrecacheController {
21 private _installAndActiveListenersAdded?;
22 private readonly _strategy;
23 private readonly _urlsToCacheKeys;
24 private readonly _urlsToCacheModes;
25 private readonly _cacheKeysToIntegrities;
26 /**
27 * Create a new PrecacheController.
28 *
29 * @param {Object} [options]
30 * @param {string} [options.cacheName] The cache to use for precaching.
31 * @param {string} [options.plugins] Plugins to use when precaching as well
32 * as responding to fetch events for precached assets.
33 * @param {boolean} [options.fallbackToNetwork=true] Whether to attempt to
34 * get the response from the network if there's a precache miss.
35 */
36 constructor({ cacheName, plugins, fallbackToNetwork, }?: PrecacheControllerOptions);
37 /**
38 * @type {workbox-precaching.PrecacheStrategy} The strategy created by this controller and
39 * used to cache assets and respond to fetch events.
40 */
41 get strategy(): Strategy;
42 /**
43 * Adds items to the precache list, removing any duplicates and
44 * stores the files in the
45 * {@link workbox-core.cacheNames|"precache cache"} when the service
46 * worker installs.
47 *
48 * This method can be called multiple times.
49 *
50 * @param {Array<Object|string>} [entries=[]] Array of entries to precache.
51 */
52 precache(entries: Array<PrecacheEntry | string>): void;
53 /**
54 * This method will add items to the precache list, removing duplicates
55 * and ensuring the information is valid.
56 *
57 * @param {Array<workbox-precaching.PrecacheController.PrecacheEntry|string>} entries
58 * Array of entries to precache.
59 */
60 addToCacheList(entries: Array<PrecacheEntry | string>): void;
61 /**
62 * Precaches new and updated assets. Call this method from the service worker
63 * install event.
64 *
65 * Note: this method calls `event.waitUntil()` for you, so you do not need
66 * to call it yourself in your event handlers.
67 *
68 * @param {ExtendableEvent} event
69 * @return {Promise<workbox-precaching.InstallResult>}
70 */
71 install(event: ExtendableEvent): Promise<InstallResult>;
72 /**
73 * Deletes assets that are no longer present in the current precache manifest.
74 * Call this method from the service worker activate event.
75 *
76 * Note: this method calls `event.waitUntil()` for you, so you do not need
77 * to call it yourself in your event handlers.
78 *
79 * @param {ExtendableEvent} event
80 * @return {Promise<workbox-precaching.CleanupResult>}
81 */
82 activate(event: ExtendableEvent): Promise<CleanupResult>;
83 /**
84 * Returns a mapping of a precached URL to the corresponding cache key, taking
85 * into account the revision information for the URL.
86 *
87 * @return {Map<string, string>} A URL to cache key mapping.
88 */
89 getURLsToCacheKeys(): Map<string, string>;
90 /**
91 * Returns a list of all the URLs that have been precached by the current
92 * service worker.
93 *
94 * @return {Array<string>} The precached URLs.
95 */
96 getCachedURLs(): Array<string>;
97 /**
98 * Returns the cache key used for storing a given URL. If that URL is
99 * unversioned, like `/index.html', then the cache key will be the original
100 * URL with a search parameter appended to it.
101 *
102 * @param {string} url A URL whose cache key you want to look up.
103 * @return {string} The versioned URL that corresponds to a cache key
104 * for the original URL, or undefined if that URL isn't precached.
105 */
106 getCacheKeyForURL(url: string): string | undefined;
107 /**
108 * @param {string} url A cache key whose SRI you want to look up.
109 * @return {string} The subresource integrity associated with the cache key,
110 * or undefined if it's not set.
111 */
112 getIntegrityForCacheKey(cacheKey: string): string | undefined;
113 /**
114 * This acts as a drop-in replacement for
115 * [`cache.match()`](https://developer.mozilla.org/en-US/docs/Web/API/Cache/match)
116 * with the following differences:
117 *
118 * - It knows what the name of the precache is, and only checks in that cache.
119 * - It allows you to pass in an "original" URL without versioning parameters,
120 * and it will automatically look up the correct cache key for the currently
121 * active revision of that URL.
122 *
123 * E.g., `matchPrecache('index.html')` will find the correct precached
124 * response for the currently active service worker, even if the actual cache
125 * key is `'/index.html?__WB_REVISION__=1234abcd'`.
126 *
127 * @param {string|Request} request The key (without revisioning parameters)
128 * to look up in the precache.
129 * @return {Promise<Response|undefined>}
130 */
131 matchPrecache(request: string | Request): Promise<Response | undefined>;
132 /**
133 * Returns a function that looks up `url` in the precache (taking into
134 * account revision information), and returns the corresponding `Response`.
135 *
136 * @param {string} url The precached URL which will be used to lookup the
137 * `Response`.
138 * @return {workbox-routing~handlerCallback}
139 */
140 createHandlerBoundToURL(url: string): RouteHandlerCallback;
141}
142export { PrecacheController };