enable.mjs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 {isSupported} from './isSupported.mjs';
  9. import './_version.mjs';
  10. /**
  11. * If the browser supports Navigation Preload, then this will enable it.
  12. *
  13. * @param {string} [headerValue] Optionally, allows developers to
  14. * [override](https://developers.google.com/web/updates/2017/02/navigation-preload#changing_the_header)
  15. * the value of the `Service-Worker-Navigation-Preload` header which will be
  16. * sent to the server when making the navigation request.
  17. *
  18. * @memberof workbox.navigationPreload
  19. */
  20. function enable(headerValue) {
  21. if (isSupported()) {
  22. self.addEventListener('activate', (event) => {
  23. event.waitUntil(
  24. self.registration.navigationPreload.enable().then(() => {
  25. // Defaults to Service-Worker-Navigation-Preload: true if not set.
  26. if (headerValue) {
  27. self.registration.navigationPreload.setHeaderValue(headerValue);
  28. }
  29. if (process.env.NODE_ENV !== 'production') {
  30. logger.log(`Navigation preload is enabled.`);
  31. }
  32. })
  33. );
  34. });
  35. } else {
  36. if (process.env.NODE_ENV !== 'production') {
  37. logger.log(`Navigation preload is not supported in this browser.`);
  38. }
  39. }
  40. }
  41. export {enable};