setCacheNameDetails.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. Copyright 2019 Google LLC
  3. Use of this source code is governed by an MIT-style
  4. license that can be found in the LICENSE file or at
  5. https://opensource.org/licenses/MIT.
  6. */
  7. import { assert } from './_private/assert.js';
  8. import { cacheNames } from './_private/cacheNames.js';
  9. import { WorkboxError } from './_private/WorkboxError.js';
  10. import './_version.js';
  11. /**
  12. * Modifies the default cache names used by the Workbox packages.
  13. * Cache names are generated as `<prefix>-<Cache Name>-<suffix>`.
  14. *
  15. * @param {Object} details
  16. * @param {Object} [details.prefix] The string to add to the beginning of
  17. * the precache and runtime cache names.
  18. * @param {Object} [details.suffix] The string to add to the end of
  19. * the precache and runtime cache names.
  20. * @param {Object} [details.precache] The cache name to use for precache
  21. * caching.
  22. * @param {Object} [details.runtime] The cache name to use for runtime caching.
  23. * @param {Object} [details.googleAnalytics] The cache name to use for
  24. * `workbox-google-analytics` caching.
  25. *
  26. * @memberof module:workbox-core
  27. */
  28. function setCacheNameDetails(details) {
  29. if (process.env.NODE_ENV !== 'production') {
  30. Object.keys(details).forEach((key) => {
  31. assert.isType(details[key], 'string', {
  32. moduleName: 'workbox-core',
  33. funcName: 'setCacheNameDetails',
  34. paramName: `details.${key}`,
  35. });
  36. });
  37. if ('precache' in details && details['precache'].length === 0) {
  38. throw new WorkboxError('invalid-cache-name', {
  39. cacheNameId: 'precache',
  40. value: details['precache'],
  41. });
  42. }
  43. if ('runtime' in details && details['runtime'].length === 0) {
  44. throw new WorkboxError('invalid-cache-name', {
  45. cacheNameId: 'runtime',
  46. value: details['runtime'],
  47. });
  48. }
  49. if ('googleAnalytics' in details && details['googleAnalytics'].length === 0) {
  50. throw new WorkboxError('invalid-cache-name', {
  51. cacheNameId: 'googleAnalytics',
  52. value: details['googleAnalytics'],
  53. });
  54. }
  55. }
  56. cacheNames.updateDetails(details);
  57. }
  58. export { setCacheNameDetails };