workbox-recipes.dev.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. this.workbox = this.workbox || {};
  2. this.workbox.recipes = (function (exports, registerRoute_js, StaleWhileRevalidate_js, CacheFirst_js, CacheableResponsePlugin_js, ExpirationPlugin_js, NetworkFirst_js, setCatchHandler_js, matchPrecache_js) {
  3. 'use strict';
  4. try {
  5. self['workbox:recipes:6.6.0'] && _();
  6. } catch (e) {}
  7. /*
  8. Copyright 2020 Google LLC
  9. Use of this source code is governed by an MIT-style
  10. license that can be found in the LICENSE file or at
  11. https://opensource.org/licenses/MIT.
  12. */
  13. /**
  14. * An implementation of the [Google fonts]{@link https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts} caching recipe
  15. *
  16. * @memberof workbox-recipes
  17. *
  18. * @param {Object} [options]
  19. * @param {string} [options.cachePrefix] Cache prefix for caching stylesheets and webfonts. Defaults to google-fonts
  20. * @param {number} [options.maxAgeSeconds] Maximum age, in seconds, that font entries will be cached for. Defaults to 1 year
  21. * @param {number} [options.maxEntries] Maximum number of fonts that will be cached. Defaults to 30
  22. */
  23. function googleFontsCache(options = {}) {
  24. const sheetCacheName = `${options.cachePrefix || 'google-fonts'}-stylesheets`;
  25. const fontCacheName = `${options.cachePrefix || 'google-fonts'}-webfonts`;
  26. const maxAgeSeconds = options.maxAgeSeconds || 60 * 60 * 24 * 365;
  27. const maxEntries = options.maxEntries || 30; // Cache the Google Fonts stylesheets with a stale-while-revalidate strategy.
  28. registerRoute_js.registerRoute(({
  29. url
  30. }) => url.origin === 'https://fonts.googleapis.com', new StaleWhileRevalidate_js.StaleWhileRevalidate({
  31. cacheName: sheetCacheName
  32. })); // Cache the underlying font files with a cache-first strategy for 1 year.
  33. registerRoute_js.registerRoute(({
  34. url
  35. }) => url.origin === 'https://fonts.gstatic.com', new CacheFirst_js.CacheFirst({
  36. cacheName: fontCacheName,
  37. plugins: [new CacheableResponsePlugin_js.CacheableResponsePlugin({
  38. statuses: [0, 200]
  39. }), new ExpirationPlugin_js.ExpirationPlugin({
  40. maxAgeSeconds,
  41. maxEntries
  42. })]
  43. }));
  44. }
  45. /**
  46. * @memberof workbox-recipes
  47. * @param {Object} options
  48. * @param {string[]} options.urls Paths to warm the strategy's cache with
  49. * @param {Strategy} options.strategy Strategy to use
  50. */
  51. function warmStrategyCache(options) {
  52. self.addEventListener('install', event => {
  53. const done = options.urls.map(path => options.strategy.handleAll({
  54. event,
  55. request: new Request(path)
  56. })[1]);
  57. event.waitUntil(Promise.all(done));
  58. });
  59. }
  60. /*
  61. Copyright 2020 Google LLC
  62. Use of this source code is governed by an MIT-style
  63. license that can be found in the LICENSE file or at
  64. https://opensource.org/licenses/MIT.
  65. */
  66. /**
  67. * An implementation of the [image caching recipe]{@link https://developers.google.com/web/tools/workbox/guides/common-recipes#caching_images}
  68. *
  69. * @memberof workbox-recipes
  70. *
  71. * @param {Object} [options]
  72. * @param {string} [options.cacheName] Name for cache. Defaults to images
  73. * @param {RouteMatchCallback} [options.matchCallback] Workbox callback function to call to match to. Defaults to request.destination === 'image';
  74. * @param {number} [options.maxAgeSeconds] Maximum age, in seconds, that font entries will be cached for. Defaults to 30 days
  75. * @param {number} [options.maxEntries] Maximum number of images that will be cached. Defaults to 60
  76. * @param {WorkboxPlugin[]} [options.plugins] Additional plugins to use for this recipe
  77. * @param {string[]} [options.warmCache] Paths to call to use to warm this cache
  78. */
  79. function imageCache(options = {}) {
  80. const defaultMatchCallback = ({
  81. request
  82. }) => request.destination === 'image';
  83. const cacheName = options.cacheName || 'images';
  84. const matchCallback = options.matchCallback || defaultMatchCallback;
  85. const maxAgeSeconds = options.maxAgeSeconds || 30 * 24 * 60 * 60;
  86. const maxEntries = options.maxEntries || 60;
  87. const plugins = options.plugins || [];
  88. plugins.push(new CacheableResponsePlugin_js.CacheableResponsePlugin({
  89. statuses: [0, 200]
  90. }));
  91. plugins.push(new ExpirationPlugin_js.ExpirationPlugin({
  92. maxEntries,
  93. maxAgeSeconds
  94. }));
  95. const strategy = new CacheFirst_js.CacheFirst({
  96. cacheName,
  97. plugins
  98. });
  99. registerRoute_js.registerRoute(matchCallback, strategy); // Warms the cache
  100. if (options.warmCache) {
  101. warmStrategyCache({
  102. urls: options.warmCache,
  103. strategy
  104. });
  105. }
  106. }
  107. /*
  108. Copyright 2020 Google LLC
  109. Use of this source code is governed by an MIT-style
  110. license that can be found in the LICENSE file or at
  111. https://opensource.org/licenses/MIT.
  112. */
  113. /**
  114. * An implementation of the [CSS and JavaScript files recipe]{@link https://developers.google.com/web/tools/workbox/guides/common-recipes#cache_css_and_javascript_files}
  115. *
  116. * @memberof workbox-recipes
  117. *
  118. * @param {Object} [options]
  119. * @param {string} [options.cacheName] Name for cache. Defaults to static-resources
  120. * @param {RouteMatchCallback} [options.matchCallback] Workbox callback function to call to match to. Defaults to request.destination === 'style' || request.destination === 'script' || request.destination === 'worker';
  121. * @param {WorkboxPlugin[]} [options.plugins] Additional plugins to use for this recipe
  122. * @param {string[]} [options.warmCache] Paths to call to use to warm this cache
  123. */
  124. function staticResourceCache(options = {}) {
  125. const defaultMatchCallback = ({
  126. request
  127. }) => request.destination === 'style' || request.destination === 'script' || request.destination === 'worker';
  128. const cacheName = options.cacheName || 'static-resources';
  129. const matchCallback = options.matchCallback || defaultMatchCallback;
  130. const plugins = options.plugins || [];
  131. plugins.push(new CacheableResponsePlugin_js.CacheableResponsePlugin({
  132. statuses: [0, 200]
  133. }));
  134. const strategy = new StaleWhileRevalidate_js.StaleWhileRevalidate({
  135. cacheName,
  136. plugins
  137. });
  138. registerRoute_js.registerRoute(matchCallback, strategy); // Warms the cache
  139. if (options.warmCache) {
  140. warmStrategyCache({
  141. urls: options.warmCache,
  142. strategy
  143. });
  144. }
  145. }
  146. /*
  147. Copyright 2020 Google LLC
  148. Use of this source code is governed by an MIT-style
  149. license that can be found in the LICENSE file or at
  150. https://opensource.org/licenses/MIT.
  151. */
  152. /**
  153. * An implementation of a page caching recipe with a network timeout
  154. *
  155. * @memberof workbox-recipes
  156. *
  157. * @param {Object} [options]
  158. * @param {string} [options.cacheName] Name for cache. Defaults to pages
  159. * @param {RouteMatchCallback} [options.matchCallback] Workbox callback function to call to match to. Defaults to request.mode === 'navigate';
  160. * @param {number} [options.networkTimoutSeconds] Maximum amount of time, in seconds, to wait on the network before falling back to cache. Defaults to 3
  161. * @param {WorkboxPlugin[]} [options.plugins] Additional plugins to use for this recipe
  162. * @param {string[]} [options.warmCache] Paths to call to use to warm this cache
  163. */
  164. function pageCache(options = {}) {
  165. const defaultMatchCallback = ({
  166. request
  167. }) => request.mode === 'navigate';
  168. const cacheName = options.cacheName || 'pages';
  169. const matchCallback = options.matchCallback || defaultMatchCallback;
  170. const networkTimeoutSeconds = options.networkTimeoutSeconds || 3;
  171. const plugins = options.plugins || [];
  172. plugins.push(new CacheableResponsePlugin_js.CacheableResponsePlugin({
  173. statuses: [0, 200]
  174. }));
  175. const strategy = new NetworkFirst_js.NetworkFirst({
  176. networkTimeoutSeconds,
  177. cacheName,
  178. plugins
  179. }); // Registers the route
  180. registerRoute_js.registerRoute(matchCallback, strategy); // Warms the cache
  181. if (options.warmCache) {
  182. warmStrategyCache({
  183. urls: options.warmCache,
  184. strategy
  185. });
  186. }
  187. }
  188. /*
  189. Copyright 2020 Google LLC
  190. Use of this source code is governed by an MIT-style
  191. license that can be found in the LICENSE file or at
  192. https://opensource.org/licenses/MIT.
  193. */
  194. /**
  195. * An implementation of the [comprehensive fallbacks recipe]{@link https://developers.google.com/web/tools/workbox/guides/advanced-recipes#comprehensive_fallbacks}. Be sure to include the fallbacks in your precache injection
  196. *
  197. * @memberof workbox-recipes
  198. *
  199. * @param {Object} [options]
  200. * @param {string} [options.pageFallback] Precache name to match for pag fallbacks. Defaults to offline.html
  201. * @param {string} [options.imageFallback] Precache name to match for image fallbacks.
  202. * @param {string} [options.fontFallback] Precache name to match for font fallbacks.
  203. */
  204. function offlineFallback(options = {}) {
  205. const pageFallback = options.pageFallback || 'offline.html';
  206. const imageFallback = options.imageFallback || false;
  207. const fontFallback = options.fontFallback || false;
  208. self.addEventListener('install', event => {
  209. const files = [pageFallback];
  210. if (imageFallback) {
  211. files.push(imageFallback);
  212. }
  213. if (fontFallback) {
  214. files.push(fontFallback);
  215. }
  216. event.waitUntil(self.caches.open('workbox-offline-fallbacks').then(cache => cache.addAll(files)));
  217. });
  218. const handler = async options => {
  219. const dest = options.request.destination;
  220. const cache = await self.caches.open('workbox-offline-fallbacks');
  221. if (dest === 'document') {
  222. const match = (await matchPrecache_js.matchPrecache(pageFallback)) || (await cache.match(pageFallback));
  223. return match || Response.error();
  224. }
  225. if (dest === 'image' && imageFallback !== false) {
  226. const match = (await matchPrecache_js.matchPrecache(imageFallback)) || (await cache.match(imageFallback));
  227. return match || Response.error();
  228. }
  229. if (dest === 'font' && fontFallback !== false) {
  230. const match = (await matchPrecache_js.matchPrecache(fontFallback)) || (await cache.match(fontFallback));
  231. return match || Response.error();
  232. }
  233. return Response.error();
  234. };
  235. setCatchHandler_js.setCatchHandler(handler);
  236. }
  237. exports.googleFontsCache = googleFontsCache;
  238. exports.imageCache = imageCache;
  239. exports.offlineFallback = offlineFallback;
  240. exports.pageCache = pageCache;
  241. exports.staticResourceCache = staticResourceCache;
  242. exports.warmStrategyCache = warmStrategyCache;
  243. return exports;
  244. }({}, workbox.routing, workbox.strategies, workbox.strategies, workbox.cacheableResponse, workbox.expiration, workbox.strategies, workbox.routing, workbox.precaching));
  245. //# sourceMappingURL=workbox-recipes.dev.js.map