workbox-precaching.dev.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. this.workbox = this.workbox || {};
  2. this.workbox.precaching = (function (exports, assert_mjs, cacheNames_mjs, getFriendlyURL_mjs, logger_mjs, cacheWrapper_mjs, fetchWrapper_mjs, WorkboxError_mjs) {
  3. 'use strict';
  4. try {
  5. self['workbox:precaching:4.3.1'] && _();
  6. } catch (e) {} // eslint-disable-line
  7. /*
  8. Copyright 2019 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. const plugins = [];
  14. const precachePlugins = {
  15. /*
  16. * @return {Array}
  17. * @private
  18. */
  19. get() {
  20. return plugins;
  21. },
  22. /*
  23. * @param {Array} newPlugins
  24. * @private
  25. */
  26. add(newPlugins) {
  27. plugins.push(...newPlugins);
  28. }
  29. };
  30. /*
  31. Copyright 2019 Google LLC
  32. Use of this source code is governed by an MIT-style
  33. license that can be found in the LICENSE file or at
  34. https://opensource.org/licenses/MIT.
  35. */
  36. /**
  37. * Adds plugins to precaching.
  38. *
  39. * @param {Array<Object>} newPlugins
  40. *
  41. * @alias workbox.precaching.addPlugins
  42. */
  43. const addPlugins = newPlugins => {
  44. precachePlugins.add(newPlugins);
  45. };
  46. /*
  47. Copyright 2018 Google LLC
  48. Use of this source code is governed by an MIT-style
  49. license that can be found in the LICENSE file or at
  50. https://opensource.org/licenses/MIT.
  51. */
  52. /**
  53. * @param {Response} response
  54. * @return {Response}
  55. *
  56. * @private
  57. * @memberof module:workbox-precaching
  58. */
  59. async function cleanRedirect(response) {
  60. const clonedResponse = response.clone(); // Not all browsers support the Response.body stream, so fall back
  61. // to reading the entire body into memory as a blob.
  62. const bodyPromise = 'body' in clonedResponse ? Promise.resolve(clonedResponse.body) : clonedResponse.blob();
  63. const body = await bodyPromise; // new Response() is happy when passed either a stream or a Blob.
  64. return new Response(body, {
  65. headers: clonedResponse.headers,
  66. status: clonedResponse.status,
  67. statusText: clonedResponse.statusText
  68. });
  69. }
  70. /*
  71. Copyright 2018 Google LLC
  72. Use of this source code is governed by an MIT-style
  73. license that can be found in the LICENSE file or at
  74. https://opensource.org/licenses/MIT.
  75. */
  76. const REVISION_SEARCH_PARAM = '__WB_REVISION__';
  77. /**
  78. * Converts a manifest entry into a versioned URL suitable for precaching.
  79. *
  80. * @param {Object} entry
  81. * @return {string} A URL with versioning info.
  82. *
  83. * @private
  84. * @memberof module:workbox-precaching
  85. */
  86. function createCacheKey(entry) {
  87. if (!entry) {
  88. throw new WorkboxError_mjs.WorkboxError('add-to-cache-list-unexpected-type', {
  89. entry
  90. });
  91. } // If a precache manifest entry is a string, it's assumed to be a versioned
  92. // URL, like '/app.abcd1234.js'. Return as-is.
  93. if (typeof entry === 'string') {
  94. const urlObject = new URL(entry, location);
  95. return {
  96. cacheKey: urlObject.href,
  97. url: urlObject.href
  98. };
  99. }
  100. const {
  101. revision,
  102. url
  103. } = entry;
  104. if (!url) {
  105. throw new WorkboxError_mjs.WorkboxError('add-to-cache-list-unexpected-type', {
  106. entry
  107. });
  108. } // If there's just a URL and no revision, then it's also assumed to be a
  109. // versioned URL.
  110. if (!revision) {
  111. const urlObject = new URL(url, location);
  112. return {
  113. cacheKey: urlObject.href,
  114. url: urlObject.href
  115. };
  116. } // Otherwise, construct a properly versioned URL using the custom Workbox
  117. // search parameter along with the revision info.
  118. const originalURL = new URL(url, location);
  119. const cacheKeyURL = new URL(url, location);
  120. cacheKeyURL.searchParams.set(REVISION_SEARCH_PARAM, revision);
  121. return {
  122. cacheKey: cacheKeyURL.href,
  123. url: originalURL.href
  124. };
  125. }
  126. /*
  127. Copyright 2018 Google LLC
  128. Use of this source code is governed by an MIT-style
  129. license that can be found in the LICENSE file or at
  130. https://opensource.org/licenses/MIT.
  131. */
  132. const logGroup = (groupTitle, deletedURLs) => {
  133. logger_mjs.logger.groupCollapsed(groupTitle);
  134. for (const url of deletedURLs) {
  135. logger_mjs.logger.log(url);
  136. }
  137. logger_mjs.logger.groupEnd();
  138. };
  139. /**
  140. * @param {Array<string>} deletedURLs
  141. *
  142. * @private
  143. * @memberof module:workbox-precaching
  144. */
  145. function printCleanupDetails(deletedURLs) {
  146. const deletionCount = deletedURLs.length;
  147. if (deletionCount > 0) {
  148. logger_mjs.logger.groupCollapsed(`During precaching cleanup, ` + `${deletionCount} cached ` + `request${deletionCount === 1 ? ' was' : 's were'} deleted.`);
  149. logGroup('Deleted Cache Requests', deletedURLs);
  150. logger_mjs.logger.groupEnd();
  151. }
  152. }
  153. /*
  154. Copyright 2018 Google LLC
  155. Use of this source code is governed by an MIT-style
  156. license that can be found in the LICENSE file or at
  157. https://opensource.org/licenses/MIT.
  158. */
  159. /**
  160. * @param {string} groupTitle
  161. * @param {Array<string>} urls
  162. *
  163. * @private
  164. */
  165. function _nestedGroup(groupTitle, urls) {
  166. if (urls.length === 0) {
  167. return;
  168. }
  169. logger_mjs.logger.groupCollapsed(groupTitle);
  170. for (const url of urls) {
  171. logger_mjs.logger.log(url);
  172. }
  173. logger_mjs.logger.groupEnd();
  174. }
  175. /**
  176. * @param {Array<string>} urlsToPrecache
  177. * @param {Array<string>} urlsAlreadyPrecached
  178. *
  179. * @private
  180. * @memberof module:workbox-precaching
  181. */
  182. function printInstallDetails(urlsToPrecache, urlsAlreadyPrecached) {
  183. const precachedCount = urlsToPrecache.length;
  184. const alreadyPrecachedCount = urlsAlreadyPrecached.length;
  185. if (precachedCount || alreadyPrecachedCount) {
  186. let message = `Precaching ${precachedCount} file${precachedCount === 1 ? '' : 's'}.`;
  187. if (alreadyPrecachedCount > 0) {
  188. message += ` ${alreadyPrecachedCount} ` + `file${alreadyPrecachedCount === 1 ? ' is' : 's are'} already cached.`;
  189. }
  190. logger_mjs.logger.groupCollapsed(message);
  191. _nestedGroup(`View newly precached URLs.`, urlsToPrecache);
  192. _nestedGroup(`View previously precached URLs.`, urlsAlreadyPrecached);
  193. logger_mjs.logger.groupEnd();
  194. }
  195. }
  196. /*
  197. Copyright 2018 Google LLC
  198. Use of this source code is governed by an MIT-style
  199. license that can be found in the LICENSE file or at
  200. https://opensource.org/licenses/MIT.
  201. */
  202. /**
  203. * Performs efficient precaching of assets.
  204. *
  205. * @memberof module:workbox-precaching
  206. */
  207. class PrecacheController {
  208. /**
  209. * Create a new PrecacheController.
  210. *
  211. * @param {string} [cacheName] An optional name for the cache, to override
  212. * the default precache name.
  213. */
  214. constructor(cacheName) {
  215. this._cacheName = cacheNames_mjs.cacheNames.getPrecacheName(cacheName);
  216. this._urlsToCacheKeys = new Map();
  217. }
  218. /**
  219. * This method will add items to the precache list, removing duplicates
  220. * and ensuring the information is valid.
  221. *
  222. * @param {
  223. * Array<module:workbox-precaching.PrecacheController.PrecacheEntry|string>
  224. * } entries Array of entries to precache.
  225. */
  226. addToCacheList(entries) {
  227. {
  228. assert_mjs.assert.isArray(entries, {
  229. moduleName: 'workbox-precaching',
  230. className: 'PrecacheController',
  231. funcName: 'addToCacheList',
  232. paramName: 'entries'
  233. });
  234. }
  235. for (const entry of entries) {
  236. const {
  237. cacheKey,
  238. url
  239. } = createCacheKey(entry);
  240. if (this._urlsToCacheKeys.has(url) && this._urlsToCacheKeys.get(url) !== cacheKey) {
  241. throw new WorkboxError_mjs.WorkboxError('add-to-cache-list-conflicting-entries', {
  242. firstEntry: this._urlsToCacheKeys.get(url),
  243. secondEntry: cacheKey
  244. });
  245. }
  246. this._urlsToCacheKeys.set(url, cacheKey);
  247. }
  248. }
  249. /**
  250. * Precaches new and updated assets. Call this method from the service worker
  251. * install event.
  252. *
  253. * @param {Object} options
  254. * @param {Event} [options.event] The install event (if needed).
  255. * @param {Array<Object>} [options.plugins] Plugins to be used for fetching
  256. * and caching during install.
  257. * @return {Promise<workbox.precaching.InstallResult>}
  258. */
  259. async install({
  260. event,
  261. plugins
  262. } = {}) {
  263. {
  264. if (plugins) {
  265. assert_mjs.assert.isArray(plugins, {
  266. moduleName: 'workbox-precaching',
  267. className: 'PrecacheController',
  268. funcName: 'install',
  269. paramName: 'plugins'
  270. });
  271. }
  272. }
  273. const urlsToPrecache = [];
  274. const urlsAlreadyPrecached = [];
  275. const cache = await caches.open(this._cacheName);
  276. const alreadyCachedRequests = await cache.keys();
  277. const alreadyCachedURLs = new Set(alreadyCachedRequests.map(request => request.url));
  278. for (const cacheKey of this._urlsToCacheKeys.values()) {
  279. if (alreadyCachedURLs.has(cacheKey)) {
  280. urlsAlreadyPrecached.push(cacheKey);
  281. } else {
  282. urlsToPrecache.push(cacheKey);
  283. }
  284. }
  285. const precacheRequests = urlsToPrecache.map(url => {
  286. return this._addURLToCache({
  287. event,
  288. plugins,
  289. url
  290. });
  291. });
  292. await Promise.all(precacheRequests);
  293. {
  294. printInstallDetails(urlsToPrecache, urlsAlreadyPrecached);
  295. }
  296. return {
  297. updatedURLs: urlsToPrecache,
  298. notUpdatedURLs: urlsAlreadyPrecached
  299. };
  300. }
  301. /**
  302. * Deletes assets that are no longer present in the current precache manifest.
  303. * Call this method from the service worker activate event.
  304. *
  305. * @return {Promise<workbox.precaching.CleanupResult>}
  306. */
  307. async activate() {
  308. const cache = await caches.open(this._cacheName);
  309. const currentlyCachedRequests = await cache.keys();
  310. const expectedCacheKeys = new Set(this._urlsToCacheKeys.values());
  311. const deletedURLs = [];
  312. for (const request of currentlyCachedRequests) {
  313. if (!expectedCacheKeys.has(request.url)) {
  314. await cache.delete(request);
  315. deletedURLs.push(request.url);
  316. }
  317. }
  318. {
  319. printCleanupDetails(deletedURLs);
  320. }
  321. return {
  322. deletedURLs
  323. };
  324. }
  325. /**
  326. * Requests the entry and saves it to the cache if the response is valid.
  327. * By default, any response with a status code of less than 400 (including
  328. * opaque responses) is considered valid.
  329. *
  330. * If you need to use custom criteria to determine what's valid and what
  331. * isn't, then pass in an item in `options.plugins` that implements the
  332. * `cacheWillUpdate()` lifecycle event.
  333. *
  334. * @private
  335. * @param {Object} options
  336. * @param {string} options.url The URL to fetch and cache.
  337. * @param {Event} [options.event] The install event (if passed).
  338. * @param {Array<Object>} [options.plugins] An array of plugins to apply to
  339. * fetch and caching.
  340. */
  341. async _addURLToCache({
  342. url,
  343. event,
  344. plugins
  345. }) {
  346. const request = new Request(url, {
  347. credentials: 'same-origin'
  348. });
  349. let response = await fetchWrapper_mjs.fetchWrapper.fetch({
  350. event,
  351. plugins,
  352. request
  353. }); // Allow developers to override the default logic about what is and isn't
  354. // valid by passing in a plugin implementing cacheWillUpdate(), e.g.
  355. // a workbox.cacheableResponse.Plugin instance.
  356. let cacheWillUpdateCallback;
  357. for (const plugin of plugins || []) {
  358. if ('cacheWillUpdate' in plugin) {
  359. cacheWillUpdateCallback = plugin.cacheWillUpdate.bind(plugin);
  360. }
  361. }
  362. const isValidResponse = cacheWillUpdateCallback ? // Use a callback if provided. It returns a truthy value if valid.
  363. cacheWillUpdateCallback({
  364. event,
  365. request,
  366. response
  367. }) : // Otherwise, default to considering any response status under 400 valid.
  368. // This includes, by default, considering opaque responses valid.
  369. response.status < 400; // Consider this a failure, leading to the `install` handler failing, if
  370. // we get back an invalid response.
  371. if (!isValidResponse) {
  372. throw new WorkboxError_mjs.WorkboxError('bad-precaching-response', {
  373. url,
  374. status: response.status
  375. });
  376. }
  377. if (response.redirected) {
  378. response = await cleanRedirect(response);
  379. }
  380. await cacheWrapper_mjs.cacheWrapper.put({
  381. event,
  382. plugins,
  383. request,
  384. response,
  385. cacheName: this._cacheName,
  386. matchOptions: {
  387. ignoreSearch: true
  388. }
  389. });
  390. }
  391. /**
  392. * Returns a mapping of a precached URL to the corresponding cache key, taking
  393. * into account the revision information for the URL.
  394. *
  395. * @return {Map<string, string>} A URL to cache key mapping.
  396. */
  397. getURLsToCacheKeys() {
  398. return this._urlsToCacheKeys;
  399. }
  400. /**
  401. * Returns a list of all the URLs that have been precached by the current
  402. * service worker.
  403. *
  404. * @return {Array<string>} The precached URLs.
  405. */
  406. getCachedURLs() {
  407. return [...this._urlsToCacheKeys.keys()];
  408. }
  409. /**
  410. * Returns the cache key used for storing a given URL. If that URL is
  411. * unversioned, like `/index.html', then the cache key will be the original
  412. * URL with a search parameter appended to it.
  413. *
  414. * @param {string} url A URL whose cache key you want to look up.
  415. * @return {string} The versioned URL that corresponds to a cache key
  416. * for the original URL, or undefined if that URL isn't precached.
  417. */
  418. getCacheKeyForURL(url) {
  419. const urlObject = new URL(url, location);
  420. return this._urlsToCacheKeys.get(urlObject.href);
  421. }
  422. }
  423. /*
  424. Copyright 2019 Google LLC
  425. Use of this source code is governed by an MIT-style
  426. license that can be found in the LICENSE file or at
  427. https://opensource.org/licenses/MIT.
  428. */
  429. let precacheController;
  430. /**
  431. * @return {PrecacheController}
  432. * @private
  433. */
  434. const getOrCreatePrecacheController = () => {
  435. if (!precacheController) {
  436. precacheController = new PrecacheController();
  437. }
  438. return precacheController;
  439. };
  440. /*
  441. Copyright 2018 Google LLC
  442. Use of this source code is governed by an MIT-style
  443. license that can be found in the LICENSE file or at
  444. https://opensource.org/licenses/MIT.
  445. */
  446. /**
  447. * Removes any URL search parameters that should be ignored.
  448. *
  449. * @param {URL} urlObject The original URL.
  450. * @param {Array<RegExp>} ignoreURLParametersMatching RegExps to test against
  451. * each search parameter name. Matches mean that the search parameter should be
  452. * ignored.
  453. * @return {URL} The URL with any ignored search parameters removed.
  454. *
  455. * @private
  456. * @memberof module:workbox-precaching
  457. */
  458. function removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching) {
  459. // Convert the iterable into an array at the start of the loop to make sure
  460. // deletion doesn't mess up iteration.
  461. for (const paramName of [...urlObject.searchParams.keys()]) {
  462. if (ignoreURLParametersMatching.some(regExp => regExp.test(paramName))) {
  463. urlObject.searchParams.delete(paramName);
  464. }
  465. }
  466. return urlObject;
  467. }
  468. /*
  469. Copyright 2019 Google LLC
  470. Use of this source code is governed by an MIT-style
  471. license that can be found in the LICENSE file or at
  472. https://opensource.org/licenses/MIT.
  473. */
  474. /**
  475. * Generator function that yields possible variations on the original URL to
  476. * check, one at a time.
  477. *
  478. * @param {string} url
  479. * @param {Object} options
  480. *
  481. * @private
  482. * @memberof module:workbox-precaching
  483. */
  484. function* generateURLVariations(url, {
  485. ignoreURLParametersMatching,
  486. directoryIndex,
  487. cleanURLs,
  488. urlManipulation
  489. } = {}) {
  490. const urlObject = new URL(url, location);
  491. urlObject.hash = '';
  492. yield urlObject.href;
  493. const urlWithoutIgnoredParams = removeIgnoredSearchParams(urlObject, ignoreURLParametersMatching);
  494. yield urlWithoutIgnoredParams.href;
  495. if (directoryIndex && urlWithoutIgnoredParams.pathname.endsWith('/')) {
  496. const directoryURL = new URL(urlWithoutIgnoredParams);
  497. directoryURL.pathname += directoryIndex;
  498. yield directoryURL.href;
  499. }
  500. if (cleanURLs) {
  501. const cleanURL = new URL(urlWithoutIgnoredParams);
  502. cleanURL.pathname += '.html';
  503. yield cleanURL.href;
  504. }
  505. if (urlManipulation) {
  506. const additionalURLs = urlManipulation({
  507. url: urlObject
  508. });
  509. for (const urlToAttempt of additionalURLs) {
  510. yield urlToAttempt.href;
  511. }
  512. }
  513. }
  514. /*
  515. Copyright 2019 Google LLC
  516. Use of this source code is governed by an MIT-style
  517. license that can be found in the LICENSE file or at
  518. https://opensource.org/licenses/MIT.
  519. */
  520. /**
  521. * This function will take the request URL and manipulate it based on the
  522. * configuration options.
  523. *
  524. * @param {string} url
  525. * @param {Object} options
  526. * @return {string} Returns the URL in the cache that matches the request,
  527. * if possible.
  528. *
  529. * @private
  530. */
  531. const getCacheKeyForURL = (url, options) => {
  532. const precacheController = getOrCreatePrecacheController();
  533. const urlsToCacheKeys = precacheController.getURLsToCacheKeys();
  534. for (const possibleURL of generateURLVariations(url, options)) {
  535. const possibleCacheKey = urlsToCacheKeys.get(possibleURL);
  536. if (possibleCacheKey) {
  537. return possibleCacheKey;
  538. }
  539. }
  540. };
  541. /*
  542. Copyright 2019 Google LLC
  543. Use of this source code is governed by an MIT-style
  544. license that can be found in the LICENSE file or at
  545. https://opensource.org/licenses/MIT.
  546. */
  547. /**
  548. * Adds a `fetch` listener to the service worker that will
  549. * respond to
  550. * [network requests]{@link https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers#Custom_responses_to_requests}
  551. * with precached assets.
  552. *
  553. * Requests for assets that aren't precached, the `FetchEvent` will not be
  554. * responded to, allowing the event to fall through to other `fetch` event
  555. * listeners.
  556. *
  557. * NOTE: when called more than once this method will replace the previously set
  558. * configuration options. Calling it more than once is not recommended outside
  559. * of tests.
  560. *
  561. * @private
  562. * @param {Object} options
  563. * @param {string} [options.directoryIndex=index.html] The `directoryIndex` will
  564. * check cache entries for a URLs ending with '/' to see if there is a hit when
  565. * appending the `directoryIndex` value.
  566. * @param {Array<RegExp>} [options.ignoreURLParametersMatching=[/^utm_/]] An
  567. * array of regex's to remove search params when looking for a cache match.
  568. * @param {boolean} [options.cleanURLs=true] The `cleanURLs` option will
  569. * check the cache for the URL with a `.html` added to the end of the end.
  570. * @param {workbox.precaching~urlManipulation} [options.urlManipulation]
  571. * This is a function that should take a URL and return an array of
  572. * alternative URL's that should be checked for precache matches.
  573. */
  574. const addFetchListener = ({
  575. ignoreURLParametersMatching = [/^utm_/],
  576. directoryIndex = 'index.html',
  577. cleanURLs = true,
  578. urlManipulation = null
  579. } = {}) => {
  580. const cacheName = cacheNames_mjs.cacheNames.getPrecacheName();
  581. addEventListener('fetch', event => {
  582. const precachedURL = getCacheKeyForURL(event.request.url, {
  583. cleanURLs,
  584. directoryIndex,
  585. ignoreURLParametersMatching,
  586. urlManipulation
  587. });
  588. if (!precachedURL) {
  589. {
  590. logger_mjs.logger.debug(`Precaching did not find a match for ` + getFriendlyURL_mjs.getFriendlyURL(event.request.url));
  591. }
  592. return;
  593. }
  594. let responsePromise = caches.open(cacheName).then(cache => {
  595. return cache.match(precachedURL);
  596. }).then(cachedResponse => {
  597. if (cachedResponse) {
  598. return cachedResponse;
  599. } // Fall back to the network if we don't have a cached response
  600. // (perhaps due to manual cache cleanup).
  601. {
  602. logger_mjs.logger.warn(`The precached response for ` + `${getFriendlyURL_mjs.getFriendlyURL(precachedURL)} in ${cacheName} was not found. ` + `Falling back to the network instead.`);
  603. }
  604. return fetch(precachedURL);
  605. });
  606. {
  607. responsePromise = responsePromise.then(response => {
  608. // Workbox is going to handle the route.
  609. // print the routing details to the console.
  610. logger_mjs.logger.groupCollapsed(`Precaching is responding to: ` + getFriendlyURL_mjs.getFriendlyURL(event.request.url));
  611. logger_mjs.logger.log(`Serving the precached url: ${precachedURL}`);
  612. logger_mjs.logger.groupCollapsed(`View request details here.`);
  613. logger_mjs.logger.log(event.request);
  614. logger_mjs.logger.groupEnd();
  615. logger_mjs.logger.groupCollapsed(`View response details here.`);
  616. logger_mjs.logger.log(response);
  617. logger_mjs.logger.groupEnd();
  618. logger_mjs.logger.groupEnd();
  619. return response;
  620. });
  621. }
  622. event.respondWith(responsePromise);
  623. });
  624. };
  625. /*
  626. Copyright 2019 Google LLC
  627. Use of this source code is governed by an MIT-style
  628. license that can be found in the LICENSE file or at
  629. https://opensource.org/licenses/MIT.
  630. */
  631. let listenerAdded = false;
  632. /**
  633. * Add a `fetch` listener to the service worker that will
  634. * respond to
  635. * [network requests]{@link https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers#Custom_responses_to_requests}
  636. * with precached assets.
  637. *
  638. * Requests for assets that aren't precached, the `FetchEvent` will not be
  639. * responded to, allowing the event to fall through to other `fetch` event
  640. * listeners.
  641. *
  642. * @param {Object} options
  643. * @param {string} [options.directoryIndex=index.html] The `directoryIndex` will
  644. * check cache entries for a URLs ending with '/' to see if there is a hit when
  645. * appending the `directoryIndex` value.
  646. * @param {Array<RegExp>} [options.ignoreURLParametersMatching=[/^utm_/]] An
  647. * array of regex's to remove search params when looking for a cache match.
  648. * @param {boolean} [options.cleanURLs=true] The `cleanURLs` option will
  649. * check the cache for the URL with a `.html` added to the end of the end.
  650. * @param {workbox.precaching~urlManipulation} [options.urlManipulation]
  651. * This is a function that should take a URL and return an array of
  652. * alternative URL's that should be checked for precache matches.
  653. *
  654. * @alias workbox.precaching.addRoute
  655. */
  656. const addRoute = options => {
  657. if (!listenerAdded) {
  658. addFetchListener(options);
  659. listenerAdded = true;
  660. }
  661. };
  662. /*
  663. Copyright 2018 Google LLC
  664. Use of this source code is governed by an MIT-style
  665. license that can be found in the LICENSE file or at
  666. https://opensource.org/licenses/MIT.
  667. */
  668. const SUBSTRING_TO_FIND = '-precache-';
  669. /**
  670. * Cleans up incompatible precaches that were created by older versions of
  671. * Workbox, by a service worker registered under the current scope.
  672. *
  673. * This is meant to be called as part of the `activate` event.
  674. *
  675. * This should be safe to use as long as you don't include `substringToFind`
  676. * (defaulting to `-precache-`) in your non-precache cache names.
  677. *
  678. * @param {string} currentPrecacheName The cache name currently in use for
  679. * precaching. This cache won't be deleted.
  680. * @param {string} [substringToFind='-precache-'] Cache names which include this
  681. * substring will be deleted (excluding `currentPrecacheName`).
  682. * @return {Array<string>} A list of all the cache names that were deleted.
  683. *
  684. * @private
  685. * @memberof module:workbox-precaching
  686. */
  687. const deleteOutdatedCaches = async (currentPrecacheName, substringToFind = SUBSTRING_TO_FIND) => {
  688. const cacheNames = await caches.keys();
  689. const cacheNamesToDelete = cacheNames.filter(cacheName => {
  690. return cacheName.includes(substringToFind) && cacheName.includes(self.registration.scope) && cacheName !== currentPrecacheName;
  691. });
  692. await Promise.all(cacheNamesToDelete.map(cacheName => caches.delete(cacheName)));
  693. return cacheNamesToDelete;
  694. };
  695. /*
  696. Copyright 2019 Google LLC
  697. Use of this source code is governed by an MIT-style
  698. license that can be found in the LICENSE file or at
  699. https://opensource.org/licenses/MIT.
  700. */
  701. /**
  702. * Adds an `activate` event listener which will clean up incompatible
  703. * precaches that were created by older versions of Workbox.
  704. *
  705. * @alias workbox.precaching.cleanupOutdatedCaches
  706. */
  707. const cleanupOutdatedCaches = () => {
  708. addEventListener('activate', event => {
  709. const cacheName = cacheNames_mjs.cacheNames.getPrecacheName();
  710. event.waitUntil(deleteOutdatedCaches(cacheName).then(cachesDeleted => {
  711. {
  712. if (cachesDeleted.length > 0) {
  713. logger_mjs.logger.log(`The following out-of-date precaches were cleaned up ` + `automatically:`, cachesDeleted);
  714. }
  715. }
  716. }));
  717. });
  718. };
  719. /*
  720. Copyright 2019 Google LLC
  721. Use of this source code is governed by an MIT-style
  722. license that can be found in the LICENSE file or at
  723. https://opensource.org/licenses/MIT.
  724. */
  725. /**
  726. * Takes in a URL, and returns the corresponding URL that could be used to
  727. * lookup the entry in the precache.
  728. *
  729. * If a relative URL is provided, the location of the service worker file will
  730. * be used as the base.
  731. *
  732. * For precached entries without revision information, the cache key will be the
  733. * same as the original URL.
  734. *
  735. * For precached entries with revision information, the cache key will be the
  736. * original URL with the addition of a query parameter used for keeping track of
  737. * the revision info.
  738. *
  739. * @param {string} url The URL whose cache key to look up.
  740. * @return {string} The cache key that corresponds to that URL.
  741. *
  742. * @alias workbox.precaching.getCacheKeyForURL
  743. */
  744. const getCacheKeyForURL$1 = url => {
  745. const precacheController = getOrCreatePrecacheController();
  746. return precacheController.getCacheKeyForURL(url);
  747. };
  748. /*
  749. Copyright 2019 Google LLC
  750. Use of this source code is governed by an MIT-style
  751. license that can be found in the LICENSE file or at
  752. https://opensource.org/licenses/MIT.
  753. */
  754. const installListener = event => {
  755. const precacheController = getOrCreatePrecacheController();
  756. const plugins = precachePlugins.get();
  757. event.waitUntil(precacheController.install({
  758. event,
  759. plugins
  760. }).catch(error => {
  761. {
  762. logger_mjs.logger.error(`Service worker installation failed. It will ` + `be retried automatically during the next navigation.`);
  763. } // Re-throw the error to ensure installation fails.
  764. throw error;
  765. }));
  766. };
  767. const activateListener = event => {
  768. const precacheController = getOrCreatePrecacheController();
  769. const plugins = precachePlugins.get();
  770. event.waitUntil(precacheController.activate({
  771. event,
  772. plugins
  773. }));
  774. };
  775. /**
  776. * Adds items to the precache list, removing any duplicates and
  777. * stores the files in the
  778. * ["precache cache"]{@link module:workbox-core.cacheNames} when the service
  779. * worker installs.
  780. *
  781. * This method can be called multiple times.
  782. *
  783. * Please note: This method **will not** serve any of the cached files for you.
  784. * It only precaches files. To respond to a network request you call
  785. * [addRoute()]{@link module:workbox-precaching.addRoute}.
  786. *
  787. * If you have a single array of files to precache, you can just call
  788. * [precacheAndRoute()]{@link module:workbox-precaching.precacheAndRoute}.
  789. *
  790. * @param {Array<Object|string>} entries Array of entries to precache.
  791. *
  792. * @alias workbox.precaching.precache
  793. */
  794. const precache = entries => {
  795. const precacheController = getOrCreatePrecacheController();
  796. precacheController.addToCacheList(entries);
  797. if (entries.length > 0) {
  798. // NOTE: these listeners will only be added once (even if the `precache()`
  799. // method is called multiple times) because event listeners are implemented
  800. // as a set, where each listener must be unique.
  801. addEventListener('install', installListener);
  802. addEventListener('activate', activateListener);
  803. }
  804. };
  805. /*
  806. Copyright 2019 Google LLC
  807. Use of this source code is governed by an MIT-style
  808. license that can be found in the LICENSE file or at
  809. https://opensource.org/licenses/MIT.
  810. */
  811. /**
  812. * This method will add entries to the precache list and add a route to
  813. * respond to fetch events.
  814. *
  815. * This is a convenience method that will call
  816. * [precache()]{@link module:workbox-precaching.precache} and
  817. * [addRoute()]{@link module:workbox-precaching.addRoute} in a single call.
  818. *
  819. * @param {Array<Object|string>} entries Array of entries to precache.
  820. * @param {Object} options See
  821. * [addRoute() options]{@link module:workbox-precaching.addRoute}.
  822. *
  823. * @alias workbox.precaching.precacheAndRoute
  824. */
  825. const precacheAndRoute = (entries, options) => {
  826. precache(entries);
  827. addRoute(options);
  828. };
  829. /*
  830. Copyright 2018 Google LLC
  831. Use of this source code is governed by an MIT-style
  832. license that can be found in the LICENSE file or at
  833. https://opensource.org/licenses/MIT.
  834. */
  835. {
  836. assert_mjs.assert.isSWEnv('workbox-precaching');
  837. }
  838. exports.addPlugins = addPlugins;
  839. exports.addRoute = addRoute;
  840. exports.cleanupOutdatedCaches = cleanupOutdatedCaches;
  841. exports.getCacheKeyForURL = getCacheKeyForURL$1;
  842. exports.precache = precache;
  843. exports.precacheAndRoute = precacheAndRoute;
  844. exports.PrecacheController = PrecacheController;
  845. return exports;
  846. }({}, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private, workbox.core._private));
  847. //# sourceMappingURL=workbox-precaching.dev.js.map