addRoute.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 { addFetchListener } from './utils/addFetchListener.js';
  8. import './_version.js';
  9. let listenerAdded = false;
  10. /**
  11. * Add a `fetch` listener to the service worker that will
  12. * respond to
  13. * [network requests]{@link https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers#Custom_responses_to_requests}
  14. * with precached assets.
  15. *
  16. * Requests for assets that aren't precached, the `FetchEvent` will not be
  17. * responded to, allowing the event to fall through to other `fetch` event
  18. * listeners.
  19. *
  20. * @param {Object} [options]
  21. * @param {string} [options.directoryIndex=index.html] The `directoryIndex` will
  22. * check cache entries for a URLs ending with '/' to see if there is a hit when
  23. * appending the `directoryIndex` value.
  24. * @param {Array<RegExp>} [options.ignoreURLParametersMatching=[/^utm_/]] An
  25. * array of regex's to remove search params when looking for a cache match.
  26. * @param {boolean} [options.cleanURLs=true] The `cleanURLs` option will
  27. * check the cache for the URL with a `.html` added to the end of the end.
  28. * @param {module:workbox-precaching~urlManipulation} [options.urlManipulation]
  29. * This is a function that should take a URL and return an array of
  30. * alternative URLs that should be checked for precache matches.
  31. *
  32. * @memberof module:workbox-precaching
  33. */
  34. function addRoute(options) {
  35. if (!listenerAdded) {
  36. addFetchListener(options);
  37. listenerAdded = true;
  38. }
  39. }
  40. export { addRoute };