index.mjs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 {logger} from 'workbox-core/_private/logger.mjs';
  8. import {CacheFirst} from './CacheFirst.mjs';
  9. import {CacheOnly} from './CacheOnly.mjs';
  10. import {NetworkFirst} from './NetworkFirst.mjs';
  11. import {NetworkOnly} from './NetworkOnly.mjs';
  12. import {StaleWhileRevalidate} from './StaleWhileRevalidate.mjs';
  13. import './_version.mjs';
  14. const mapping = {
  15. cacheFirst: CacheFirst,
  16. cacheOnly: CacheOnly,
  17. networkFirst: NetworkFirst,
  18. networkOnly: NetworkOnly,
  19. staleWhileRevalidate: StaleWhileRevalidate,
  20. };
  21. const deprecate = (strategy) => {
  22. const StrategyCtr = mapping[strategy];
  23. return (options) => {
  24. if (process.env.NODE_ENV !== 'production') {
  25. const strategyCtrName = strategy[0].toUpperCase() + strategy.slice(1);
  26. logger.warn(`The 'workbox.strategies.${strategy}()' function has been ` +
  27. `deprecated and will be removed in a future version of Workbox.\n` +
  28. `Please use 'new workbox.strategies.${strategyCtrName}()' instead.`);
  29. }
  30. return new StrategyCtr(options);
  31. };
  32. };
  33. /**
  34. * @function workbox.strategies.cacheFirst
  35. * @param {Object} options See the {@link workbox.strategies.CacheFirst}
  36. * constructor for more info.
  37. * @deprecated since v4.0.0
  38. */
  39. const cacheFirst = deprecate('cacheFirst');
  40. /**
  41. * @function workbox.strategies.cacheOnly
  42. * @param {Object} options See the {@link workbox.strategies.CacheOnly}
  43. * constructor for more info.
  44. * @deprecated since v4.0.0
  45. */
  46. const cacheOnly = deprecate('cacheOnly');
  47. /**
  48. * @function workbox.strategies.networkFirst
  49. * @param {Object} options See the {@link workbox.strategies.NetworkFirst}
  50. * constructor for more info.
  51. * @deprecated since v4.0.0
  52. */
  53. const networkFirst = deprecate('networkFirst');
  54. /**
  55. * @function workbox.strategies.networkOnly
  56. * @param {Object} options See the {@link workbox.strategies.NetworkOnly}
  57. * constructor for more info.
  58. * @deprecated since v4.0.0
  59. */
  60. const networkOnly = deprecate('networkOnly');
  61. /**
  62. * @function workbox.strategies.staleWhileRevalidate
  63. * @param {Object} options See the
  64. * {@link workbox.strategies.StaleWhileRevalidate} constructor for more info.
  65. * @deprecated since v4.0.0
  66. */
  67. const staleWhileRevalidate = deprecate('staleWhileRevalidate');
  68. /**
  69. * There are common caching strategies that most service workers will need
  70. * and use. This module provides simple implementations of these strategies.
  71. *
  72. * @namespace workbox.strategies
  73. */
  74. export {
  75. CacheFirst,
  76. CacheOnly,
  77. NetworkFirst,
  78. NetworkOnly,
  79. StaleWhileRevalidate,
  80. // Deprecated...
  81. cacheFirst,
  82. cacheOnly,
  83. networkFirst,
  84. networkOnly,
  85. staleWhileRevalidate,
  86. };