cacheNames.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. Copyright 2018 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 '../_version.js';
  8. const _cacheNameDetails = {
  9. googleAnalytics: 'googleAnalytics',
  10. precache: 'precache-v2',
  11. prefix: 'workbox',
  12. runtime: 'runtime',
  13. suffix: typeof registration !== 'undefined' ? registration.scope : '',
  14. };
  15. const _createCacheName = (cacheName) => {
  16. return [_cacheNameDetails.prefix, cacheName, _cacheNameDetails.suffix]
  17. .filter((value) => value && value.length > 0)
  18. .join('-');
  19. };
  20. const eachCacheNameDetail = (fn) => {
  21. for (const key of Object.keys(_cacheNameDetails)) {
  22. fn(key);
  23. }
  24. };
  25. export const cacheNames = {
  26. updateDetails: (details) => {
  27. eachCacheNameDetail((key) => {
  28. if (typeof details[key] === 'string') {
  29. _cacheNameDetails[key] = details[key];
  30. }
  31. });
  32. },
  33. getGoogleAnalyticsName: (userCacheName) => {
  34. return userCacheName || _createCacheName(_cacheNameDetails.googleAnalytics);
  35. },
  36. getPrecacheName: (userCacheName) => {
  37. return userCacheName || _createCacheName(_cacheNameDetails.precache);
  38. },
  39. getPrefix: () => {
  40. return _cacheNameDetails.prefix;
  41. },
  42. getRuntimeName: (userCacheName) => {
  43. return userCacheName || _createCacheName(_cacheNameDetails.runtime);
  44. },
  45. getSuffix: () => {
  46. return _cacheNameDetails.suffix;
  47. },
  48. };