Skip to content

Commit 2a7294b

Browse files
authored
Add inline examples (#3038)
1 parent ab2882b commit 2a7294b

File tree

5 files changed

+97
-541
lines changed

5 files changed

+97
-541
lines changed

packages/workbox-build/src/generate-sw.ts

Lines changed: 31 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {rebasePath} from './lib/rebase-path';
1414
import {validateGenerateSWOptions} from './lib/validate-options';
1515
import {writeSWUsingDefaultTemplate} from './lib/write-sw-using-default-template';
1616

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

packages/workbox-build/src/get-manifest.ts

Lines changed: 9 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -10,81 +10,20 @@ import {getFileManifestEntries} from './lib/get-file-manifest-entries';
1010
import {GetManifestOptions, GetManifestResult} from './types';
1111
import {validateGetManifestOptions} from './lib/validate-options';
1212

13-
// eslint-disable-next-line jsdoc/newline-after-description
1413
/**
1514
* This method returns a list of URLs to precache, referred to as a "precache
1615
* manifest", along with details about the number of entries and their size,
1716
* based on the options you provide.
1817
*
19-
* @param {Object} config The configuration to use.
20-
*
21-
* @param {string} config.globDirectory The local directory you wish to match
22-
* `globPatterns` against. The path is relative to the current directory.
23-
*
24-
* @param {Array<workbox-build.ManifestEntry>} [config.additionalManifestEntries]
25-
* A list of entries to be precached, in addition to any entries that are
26-
* generated as part of the build configuration.
27-
*
28-
* @param {RegExp} [config.dontCacheBustURLsMatching] Assets that match this will be
29-
* assumed to be uniquely versioned via their URL, and exempted from the normal
30-
* HTTP cache-busting that's done when populating the precache. While not
31-
* required, it's recommended that if your existing build process already
32-
* inserts a `[hash]` value into each filename, you provide a RegExp that will
33-
* detect that, as it will reduce the bandwidth consumed when precaching.
34-
*
35-
* @param {boolean} [config.globFollow=true] Determines whether or not symlinks
36-
* are followed when generating the precache manifest. For more information, see
37-
* the definition of `follow` in the `glob`
38-
* [documentation](https://github.com/isaacs/node-glob#options).
39-
*
40-
* @param {Array<string>} [config.globIgnores=['node_modules/**']]
41-
* A set of patterns matching files to always exclude when generating the
42-
* precache manifest. For more information, see the definition of `ignore` in the `glob`
43-
* [documentation](https://github.com/isaacs/node-glob#options).
44-
*
45-
* @param {Array<string>} [config.globPatterns=['**.{js,css,html}']]
46-
* Files matching any of these patterns will be included in the precache
47-
* manifest. For more information, see the
48-
* [`glob` primer](https://github.com/isaacs/node-glob#glob-primer).
49-
*
50-
* @param {boolean} [config.globStrict=true] If true, an error reading a directory when
51-
* generating a precache manifest will cause the build to fail. If false, the
52-
* problematic directory will be skipped. For more information, see the
53-
* definition of `strict` in the `glob`
54-
* [documentation](https://github.com/isaacs/node-glob#options).
55-
*
56-
* @param {Array<workbox-build.ManifestTransform>} [config.manifestTransforms] One or more
57-
* functions which will be applied sequentially against the generated manifest.
58-
* If `modifyURLPrefix` or `dontCacheBustURLsMatching` are also specified, their
59-
* corresponding transformations will be applied first.
60-
*
61-
* @param {number} [config.maximumFileSizeToCacheInBytes=2097152] This value can be
62-
* used to determine the maximum size of files that will be precached. This
63-
* prevents you from inadvertently precaching very large files that might have
64-
* accidentally matched one of your patterns.
65-
*
66-
* @param {object<string, string>} [config.modifyURLPrefix] A mapping of prefixes
67-
* that, if present in an entry in the precache manifest, will be replaced with
68-
* the corresponding value. This can be used to, for example, remove or add a
69-
* path prefix from a manifest entry if your web hosting setup doesn't match
70-
* your local filesystem setup. As an alternative with more flexibility, you can
71-
* use the `manifestTransforms` option and provide a function that modifies the
72-
* entries in the manifest using whatever logic you provide.
73-
*
74-
* @param {Object} [config.templatedURLs] If a URL is rendered based on some
75-
* server-side logic, its contents may depend on multiple files or on some other
76-
* unique string value. The keys in this object are server-rendered URLs. If the
77-
* values are an array of strings, they will be interpreted as `glob` patterns,
78-
* and the contents of any files matching the patterns will be used to uniquely
79-
* version the URL. If used with a single string, it will be interpreted as
80-
* unique versioning information that you've generated for a given URL.
81-
*
82-
* @return {Promise<{count: number, manifestEntries: Array<workbox-build.ManifestEntry>, size: number, warnings: Array<string>}>}
83-
* A promise that resolves once the precache manifest (available in the
84-
* `manifestEntries` property) has been determined. The `size` property
85-
* contains the aggregate size of all the precached entries, in bytes, and the
86-
* `count` property contains the total number of precached entries. Any
87-
* non-fatal warning messages will be returned via `warnings`.
18+
* @example
19+
* // The following lists some common options; see the rest of the documentation
20+
* // for the full set of options and defaults.
21+
* const {count, manifestEntries, size, warnings} = await getManifest({
22+
* dontCacheBustURLsMatching: [new RegExp('...')],
23+
* globDirectory: '...',
24+
* globPatterns: ['...', '...'],
25+
* maximumFileSizeToCacheInBytes: ...,
26+
* });
8827
*
8928
* @memberof workbox-build
9029
*/

0 commit comments

Comments
 (0)