| 1 | /*
|
|---|
| 2 | Copyright 2018 Google LLC
|
|---|
| 3 |
|
|---|
| 4 | Use of this source code is governed by an MIT-style
|
|---|
| 5 | license that can be found in the LICENSE file or at
|
|---|
| 6 | https://opensource.org/licenses/MIT.
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | import template from 'lodash/template';
|
|---|
| 10 |
|
|---|
| 11 | import {errors} from './errors';
|
|---|
| 12 | import {GeneratePartial, ManifestEntry} from '../types';
|
|---|
| 13 | import {ModuleRegistry} from './module-registry';
|
|---|
| 14 | import {runtimeCachingConverter} from './runtime-caching-converter';
|
|---|
| 15 | import {stringifyWithoutComments} from './stringify-without-comments';
|
|---|
| 16 | import {swTemplate} from '../templates/sw-template';
|
|---|
| 17 |
|
|---|
| 18 | export function populateSWTemplate({
|
|---|
| 19 | cacheId,
|
|---|
| 20 | cleanupOutdatedCaches,
|
|---|
| 21 | clientsClaim,
|
|---|
| 22 | directoryIndex,
|
|---|
| 23 | disableDevLogs,
|
|---|
| 24 | ignoreURLParametersMatching,
|
|---|
| 25 | importScripts,
|
|---|
| 26 | manifestEntries = [],
|
|---|
| 27 | navigateFallback,
|
|---|
| 28 | navigateFallbackDenylist,
|
|---|
| 29 | navigateFallbackAllowlist,
|
|---|
| 30 | navigationPreload,
|
|---|
| 31 | offlineGoogleAnalytics,
|
|---|
| 32 | runtimeCaching = [],
|
|---|
| 33 | skipWaiting,
|
|---|
| 34 | }: GeneratePartial & {manifestEntries?: Array<ManifestEntry>}): string {
|
|---|
| 35 | // There needs to be at least something to precache, or else runtime caching.
|
|---|
| 36 | if (!(manifestEntries?.length > 0 || runtimeCaching.length > 0)) {
|
|---|
| 37 | throw new Error(errors['no-manifest-entries-or-runtime-caching']);
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | // These are all options that can be passed to the precacheAndRoute() method.
|
|---|
| 41 | const precacheOptions = {
|
|---|
| 42 | directoryIndex,
|
|---|
| 43 | // An array of RegExp objects can't be serialized by JSON.stringify()'s
|
|---|
| 44 | // default behavior, so if it's given, convert it manually.
|
|---|
| 45 | ignoreURLParametersMatching: ignoreURLParametersMatching
|
|---|
| 46 | ? ([] as Array<RegExp>)
|
|---|
| 47 | : undefined,
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | let precacheOptionsString = JSON.stringify(precacheOptions, null, 2);
|
|---|
| 51 | if (ignoreURLParametersMatching) {
|
|---|
| 52 | precacheOptionsString = precacheOptionsString.replace(
|
|---|
| 53 | `"ignoreURLParametersMatching": []`,
|
|---|
| 54 | `"ignoreURLParametersMatching": [` +
|
|---|
| 55 | `${ignoreURLParametersMatching.join(', ')}]`,
|
|---|
| 56 | );
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | let offlineAnalyticsConfigString: string | undefined = undefined;
|
|---|
| 60 | if (offlineGoogleAnalytics) {
|
|---|
| 61 | // If offlineGoogleAnalytics is a truthy value, we need to convert it to the
|
|---|
| 62 | // format expected by the template.
|
|---|
| 63 | offlineAnalyticsConfigString =
|
|---|
| 64 | offlineGoogleAnalytics === true
|
|---|
| 65 | ? // If it's the literal value true, then use an empty config string.
|
|---|
| 66 | '{}'
|
|---|
| 67 | : // Otherwise, convert the config object into a more complex string, taking
|
|---|
| 68 | // into account the fact that functions might need to be stringified.
|
|---|
| 69 | stringifyWithoutComments(offlineGoogleAnalytics);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | const moduleRegistry = new ModuleRegistry();
|
|---|
| 73 |
|
|---|
| 74 | try {
|
|---|
| 75 | const populatedTemplate = template(swTemplate)({
|
|---|
| 76 | cacheId,
|
|---|
| 77 | cleanupOutdatedCaches,
|
|---|
| 78 | clientsClaim,
|
|---|
| 79 | disableDevLogs,
|
|---|
| 80 | importScripts,
|
|---|
| 81 | manifestEntries,
|
|---|
| 82 | navigateFallback,
|
|---|
| 83 | navigateFallbackDenylist,
|
|---|
| 84 | navigateFallbackAllowlist,
|
|---|
| 85 | navigationPreload,
|
|---|
| 86 | offlineAnalyticsConfigString,
|
|---|
| 87 | precacheOptionsString,
|
|---|
| 88 | runtimeCaching: runtimeCachingConverter(moduleRegistry, runtimeCaching),
|
|---|
| 89 | skipWaiting,
|
|---|
| 90 | use: moduleRegistry.use.bind(moduleRegistry),
|
|---|
| 91 | });
|
|---|
| 92 |
|
|---|
| 93 | const workboxImportStatements = moduleRegistry.getImportStatements();
|
|---|
| 94 |
|
|---|
| 95 | // We need the import statements for all of the Workbox runtime modules
|
|---|
| 96 | // prepended, so that the correct bundle can be created.
|
|---|
| 97 | return workboxImportStatements.join('\n') + populatedTemplate;
|
|---|
| 98 | } catch (error) {
|
|---|
| 99 | throw new Error(
|
|---|
| 100 | `${errors['populating-sw-tmpl-failed']} '${
|
|---|
| 101 | error instanceof Error && error.message ? error.message : ''
|
|---|
| 102 | }'`,
|
|---|
| 103 | );
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|