| 1 | /*
|
|---|
| 2 | Copyright 2018 Google LLC
|
|---|
| 3 |
|
|---|
| 4 | Use of this source code is governed by an MIT-style
|
|---|
| 5 | license that can be found in the LICENSE file or at
|
|---|
| 6 | https://opensource.org/licenses/MIT.
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | import {logger} from 'workbox-core/_private/logger.js';
|
|---|
| 10 | import {isSupported} from './isSupported.js';
|
|---|
| 11 | import './_version.js';
|
|---|
| 12 |
|
|---|
| 13 | // Give TypeScript the correct global.
|
|---|
| 14 | declare let self: ServiceWorkerGlobalScope;
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * If the browser supports Navigation Preload, then this will enable it.
|
|---|
| 18 | *
|
|---|
| 19 | * @param {string} [headerValue] Optionally, allows developers to
|
|---|
| 20 | * [override](https://developers.google.com/web/updates/2017/02/navigation-preload#changing_the_header)
|
|---|
| 21 | * the value of the `Service-Worker-Navigation-Preload` header which will be
|
|---|
| 22 | * sent to the server when making the navigation request.
|
|---|
| 23 | *
|
|---|
| 24 | * @memberof workbox-navigation-preload
|
|---|
| 25 | */
|
|---|
| 26 | function enable(headerValue?: string): void {
|
|---|
| 27 | if (isSupported()) {
|
|---|
| 28 | self.addEventListener('activate', (event: ExtendableEvent) => {
|
|---|
| 29 | event.waitUntil(
|
|---|
| 30 | self.registration.navigationPreload.enable().then(() => {
|
|---|
| 31 | // Defaults to Service-Worker-Navigation-Preload: true if not set.
|
|---|
| 32 | if (headerValue) {
|
|---|
| 33 | void self.registration.navigationPreload.setHeaderValue(
|
|---|
| 34 | headerValue,
|
|---|
| 35 | );
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 39 | logger.log(`Navigation preload is enabled.`);
|
|---|
| 40 | }
|
|---|
| 41 | }),
|
|---|
| 42 | );
|
|---|
| 43 | });
|
|---|
| 44 | } else {
|
|---|
| 45 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 46 | logger.log(`Navigation preload is not supported in this browser.`);
|
|---|
| 47 | }
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | export {enable};
|
|---|