enable.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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.js';
  8. import { isSupported } from './isSupported.js';
  9. import './_version.js';
  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 module:workbox-navigation-preload
  19. */
  20. function enable(headerValue) {
  21. if (isSupported()) {
  22. self.addEventListener('activate', (event) => {
  23. event.waitUntil(self.registration.navigationPreload.enable().then(() => {
  24. // Defaults to Service-Worker-Navigation-Preload: true if not set.
  25. if (headerValue) {
  26. self.registration.navigationPreload.setHeaderValue(headerValue);
  27. }
  28. if (process.env.NODE_ENV !== 'production') {
  29. logger.log(`Navigation preload is enabled.`);
  30. }
  31. }));
  32. });
  33. }
  34. else {
  35. if (process.env.NODE_ENV !== 'production') {
  36. logger.log(`Navigation preload is not supported in this browser.`);
  37. }
  38. }
  39. }
  40. export { enable };