UNPKG

10.7 kBTypeScriptView Raw
1import { BuildResult } from './types';
2/**
3 * This method creates a list of URLs to precache, referred to as a "precache
4 * manifest", based on the options you provide.
5 *
6 * It also takes in additional options that configures the service worker's
7 * behavior, like any `runtimeCaching` rules it should use.
8 *
9 * Based on the precache manifest and the additional configuration, it writes
10 * a ready-to-use service worker file to disk at `swDest`.
11 *
12 * @param {Object} config The configuration to use.
13 *
14 * @param {string} config.globDirectory The local directory you wish to match
15 * `globPatterns` against. The path is relative to the current directory.
16 *
17 * @param {string} config.swDest The path and filename of the service worker file
18 * that will be created by the build process, relative to the current working
19 * directory. It must end in '.js'.
20 *
21 * @param {Array<module:workbox-build.ManifestEntry>} [config.additionalManifestEntries]
22 * A list of entries to be precached, in addition to any entries that are
23 * generated as part of the build configuration.
24 *
25 * @param {Array<string>} [config.babelPresetEnvTargets=['chrome >= 56']]
26 * The [targets](https://babeljs.io/docs/en/babel-preset-env#targets) to pass to
27 * `babel-preset-env` when transpiling the service worker bundle.
28 *
29 * @param {string} [config.cacheId] An optional ID to be prepended to cache
30 * names. This is primarily useful for local development where multiple sites
31 * may be served from the same `http://localhost:port` origin.
32 *
33 * @param {boolean} [config.cleanupOutdatedCaches=false] Whether or not Workbox
34 * should attempt to identify and delete any precaches created by older,
35 * incompatible versions.
36 *
37 * @param {boolean} [config.clientsClaim=false] Whether or not the service
38 * worker should [start controlling](https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#clientsclaim)
39 * any existing clients as soon as it activates.
40 *
41 * @param {string} [config.directoryIndex='index.html'] If a navigation request
42 * for a URL ending in `/` fails to match a precached URL, this value will be
43 * appended to the URL and that will be checked for a precache match. This
44 * should be set to what your web server is using for its directory index.
45 *
46 * @param {RegExp} [config.dontCacheBustURLsMatching] Assets that match this will be
47 * assumed to be uniquely versioned via their URL, and exempted from the normal
48 * HTTP cache-busting that's done when populating the precache. While not
49 * required, it's recommended that if your existing build process already
50 * inserts a `[hash]` value into each filename, you provide a RegExp that will
51 * detect that, as it will reduce the bandwidth consumed when precaching.
52 *
53 * @param {boolean} [config.globFollow=true] Determines whether or not symlinks
54 * are followed when generating the precache manifest. For more information, see
55 * the definition of `follow` in the `glob`
56 * [documentation](https://github.com/isaacs/node-glob#options).
57 *
58 * @param {Array<string>} [config.globIgnores=['node_modules/**']]
59 * A set of patterns matching files to always exclude when generating the
60 * precache manifest. For more information, see the definition of `ignore` in the `glob`
61 * [documentation](https://github.com/isaacs/node-glob#options).
62 *
63 * @param {Array<string>} [config.globPatterns=['**.{js,css,html}']]
64 * Files matching any of these patterns will be included in the precache
65 * manifest. For more information, see the
66 * [`glob` primer](https://github.com/isaacs/node-glob#glob-primer).
67 *
68 * @param {boolean} [config.globStrict=true] If true, an error reading a directory when
69 * generating a precache manifest will cause the build to fail. If false, the
70 * problematic directory will be skipped. For more information, see the
71 * definition of `strict` in the `glob`
72 * [documentation](https://github.com/isaacs/node-glob#options).
73 *
74 * @param {Array<RegExp>} [config.ignoreURLParametersMatching=[/^utm_/, /^fbclid$/]]
75 * Any search parameter names that match against one of the RegExp in this array
76 * will be removed before looking for a precache match. This is useful if your
77 * users might request URLs that contain, for example, URL parameters used to
78 * track the source of the traffic.
79 *
80 * @param {Array<string>} [config.importScripts] A list of JavaScript files that
81 * should be passed to [`importScripts()`](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts)
82 * inside the generated service worker file. This is useful when you want to
83 * let Workbox create your top-level service worker file, but want to include
84 * some additional code, such as a push event listener.
85 *
86 * @param {boolean} [config.inlineWorkboxRuntime=false] Whether the runtime code
87 * for the Workbox library should be included in the top-level service worker,
88 * or split into a separate file that needs to be deployed alongside the service
89 * worker. Keeping the runtime separate means that users will not have to
90 * re-download the Workbox code each time your top-level service worker changes.
91 *
92 * @param {Array<module:workbox-build.ManifestTransform>} [config.manifestTransforms] One or more
93 * functions which will be applied sequentially against the generated manifest.
94 * If `modifyURLPrefix` or `dontCacheBustURLsMatching` are also specified, their
95 * corresponding transformations will be applied first.
96 *
97 * @param {number} [config.maximumFileSizeToCacheInBytes=2097152] This value can be
98 * used to determine the maximum size of files that will be precached. This
99 * prevents you from inadvertently precaching very large files that might have
100 * accidentally matched one of your patterns.
101 *
102 * @param {string} [config.mode='production'] If set to 'production', then an
103 * optimized service worker bundle that excludes debugging info will be
104 * produced. If not explicitly configured here, the `process.env.NODE_ENV` value
105 * will be used, and failing that, it will fall back to `'production'`.
106 *
107 * @param {object<string, string>} [config.modifyURLPrefix] A mapping of prefixes
108 * that, if present in an entry in the precache manifest, will be replaced with
109 * the corresponding value. This can be used to, for example, remove or add a
110 * path prefix from a manifest entry if your web hosting setup doesn't match
111 * your local filesystem setup. As an alternative with more flexibility, you can
112 * use the `manifestTransforms` option and provide a function that modifies the
113 * entries in the manifest using whatever logic you provide.
114 *
115 * @param {string} [config.navigateFallback] If specified, all
116 * [navigation requests](https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests)
117 * for URLs that aren't precached will be fulfilled with the HTML at the URL
118 * provided. You must pass in the URL of an HTML document that is listed in your
119 * precache manifest. This is meant to be used in a Single Page App scenario, in
120 * which you want all navigations to use common [App Shell HTML](https://developers.google.com/web/fundamentals/architecture/app-shell).
121 *
122 * @param {Array<RegExp>} [config.navigateFallbackDenylist] An optional array
123 * of regular expressions that restricts which URLs the configured
124 * `navigateFallback` behavior applies to. This is useful if only a subset of
125 * your site's URLs should be treated as being part of a
126 * [Single Page App](https://en.wikipedia.org/wiki/Single-page_application). If
127 * both `navigateFallbackDenylist` and `navigateFallbackAllowlist` are
128 * configured, the denylist takes precedent.
129 *
130 * @param {Array<RegExp>} [config.navigateFallbackAllowlist] An optional array
131 * of regular expressions that restricts which URLs the configured
132 * `navigateFallback` behavior applies to. This is useful if only a subset of
133 * your site's URLs should be treated as being part of a
134 * [Single Page App](https://en.wikipedia.org/wiki/Single-page_application). If
135 * both `navigateFallbackDenylist` and `navigateFallbackAllowlist` are
136 * configured, the denylist takes precedent.
137 *
138 * @param {boolean} [config.navigationPreload=false] Whether or not to enable
139 * [navigation preload](https://developers.google.com/web/tools/workbox/modules/workbox-navigation-preload)
140 * in the generated service worker. When set to true, you must also use
141 * `runtimeCaching` to set up an appropriate response strategy that will match
142 * navigation requests, and make use of the preloaded response.
143 *
144 * @param {boolean|Object} [config.offlineGoogleAnalytics=false] Controls
145 * whether or not to include support for
146 * [offline Google Analytics](https://developers.google.com/web/tools/workbox/guides/enable-offline-analytics).
147 * When `true`, the call to `workbox-google-analytics`'s `initialize()` will be
148 * added to your generated service worker. When set to an `Object`, that object
149 * will be passed in to the `initialize()` call, allowing you to customize the
150 * behavior.
151 *
152 * @param {Array<RuntimeCachingEntry>} [config.runtimeCaching]
153 *
154 * @param {boolean} [config.skipWaiting=false] Whether to add an
155 * unconditional call to [`skipWaiting()`]{@link module:workbox-core.skipWaiting}
156 * to the generated service worker. If `false`, then a `message` listener will
157 * be added instead, allowing you to conditionally call `skipWaiting()` by posting
158 * a message containing {type: 'SKIP_WAITING'}.
159 *
160 * @param {boolean} [config.sourcemap=true] Whether to create a sourcemap
161 * for the generated service worker files.
162 *
163 * @param {Object} [config.templatedURLs] If a URL is rendered based on some
164 * server-side logic, its contents may depend on multiple files or on some other
165 * unique string value. The keys in this object are server-rendered URLs. If the
166 * values are an array of strings, they will be interpreted as `glob` patterns,
167 * and the contents of any files matching the patterns will be used to uniquely
168 * version the URL. If used with a single string, it will be interpreted as
169 * unique versioning information that you've generated for a given URL.
170 *
171 * @return {Promise<{count: number, filePaths: Array<string>, size: number, warnings: Array<string>}>}
172 * A promise that resolves once the service worker and related files
173 * (indicated by `filePaths`) has been written to `swDest`. The `size` property
174 * contains the aggregate size of all the precached entries, in bytes, and the
175 * `count` property contains the total number of precached entries. Any
176 * non-fatal warning messages will be returned via `warnings`.
177 *
178 * @memberof module:workbox-build
179 */
180export declare function generateSW(config: unknown): Promise<BuildResult>;