| 1 | /*
|
|---|
| 2 | Copyright 2019 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 {assert} from './_private/assert.js';
|
|---|
| 10 | import {cacheNames, PartialCacheNameDetails} from './_private/cacheNames.js';
|
|---|
| 11 | import {WorkboxError} from './_private/WorkboxError.js';
|
|---|
| 12 | import './_version.js';
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Modifies the default cache names used by the Workbox packages.
|
|---|
| 16 | * Cache names are generated as `<prefix>-<Cache Name>-<suffix>`.
|
|---|
| 17 | *
|
|---|
| 18 | * @param {Object} details
|
|---|
| 19 | * @param {Object} [details.prefix] The string to add to the beginning of
|
|---|
| 20 | * the precache and runtime cache names.
|
|---|
| 21 | * @param {Object} [details.suffix] The string to add to the end of
|
|---|
| 22 | * the precache and runtime cache names.
|
|---|
| 23 | * @param {Object} [details.precache] The cache name to use for precache
|
|---|
| 24 | * caching.
|
|---|
| 25 | * @param {Object} [details.runtime] The cache name to use for runtime caching.
|
|---|
| 26 | * @param {Object} [details.googleAnalytics] The cache name to use for
|
|---|
| 27 | * `workbox-google-analytics` caching.
|
|---|
| 28 | *
|
|---|
| 29 | * @memberof workbox-core
|
|---|
| 30 | */
|
|---|
| 31 | function setCacheNameDetails(details: PartialCacheNameDetails): void {
|
|---|
| 32 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 33 | Object.keys(details).forEach((key) => {
|
|---|
| 34 | assert!.isType(details[key], 'string', {
|
|---|
| 35 | moduleName: 'workbox-core',
|
|---|
| 36 | funcName: 'setCacheNameDetails',
|
|---|
| 37 | paramName: `details.${key}`,
|
|---|
| 38 | });
|
|---|
| 39 | });
|
|---|
| 40 |
|
|---|
| 41 | if ('precache' in details && details['precache']!.length === 0) {
|
|---|
| 42 | throw new WorkboxError('invalid-cache-name', {
|
|---|
| 43 | cacheNameId: 'precache',
|
|---|
| 44 | value: details['precache'],
|
|---|
| 45 | });
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | if ('runtime' in details && details['runtime']!.length === 0) {
|
|---|
| 49 | throw new WorkboxError('invalid-cache-name', {
|
|---|
| 50 | cacheNameId: 'runtime',
|
|---|
| 51 | value: details['runtime'],
|
|---|
| 52 | });
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | if (
|
|---|
| 56 | 'googleAnalytics' in details &&
|
|---|
| 57 | details['googleAnalytics'].length === 0
|
|---|
| 58 | ) {
|
|---|
| 59 | throw new WorkboxError('invalid-cache-name', {
|
|---|
| 60 | cacheNameId: 'googleAnalytics',
|
|---|
| 61 | value: details['googleAnalytics'],
|
|---|
| 62 | });
|
|---|
| 63 | }
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | cacheNames.updateDetails(details);
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | export {setCacheNameDetails};
|
|---|