addRoute.js 1.2 KB

12345678910111213141516171819202122232425262728293031
  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 { registerRoute } from 'workbox-routing/registerRoute.js';
  8. import { getOrCreatePrecacheController } from './utils/getOrCreatePrecacheController.js';
  9. import { PrecacheRoute } from './PrecacheRoute.js';
  10. import './_version.js';
  11. /**
  12. * Add a `fetch` listener to the service worker that will
  13. * respond to
  14. * [network requests]{@link https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers#Custom_responses_to_requests}
  15. * with precached assets.
  16. *
  17. * Requests for assets that aren't precached, the `FetchEvent` will not be
  18. * responded to, allowing the event to fall through to other `fetch` event
  19. * listeners.
  20. *
  21. * @param {Object} [options] See the {@link workbox-precaching.PrecacheRoute}
  22. * options.
  23. *
  24. * @memberof workbox-precaching
  25. */
  26. function addRoute(options) {
  27. const precacheController = getOrCreatePrecacheController();
  28. const precacheRoute = new PrecacheRoute(precacheController, options);
  29. registerRoute(precacheRoute);
  30. }
  31. export { addRoute };