| 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 | import { assert } from 'workbox-core/_private/assert.js';
|
|---|
| 9 | import { logger } from 'workbox-core/_private/logger.js';
|
|---|
| 10 | import { Route } from './Route.js';
|
|---|
| 11 | import './_version.js';
|
|---|
| 12 | /**
|
|---|
| 13 | * NavigationRoute makes it easy to create a
|
|---|
| 14 | * {@link workbox-routing.Route} that matches for browser
|
|---|
| 15 | * [navigation requests]{@link https://developers.google.com/web/fundamentals/primers/service-workers/high-performance-loading#first_what_are_navigation_requests}.
|
|---|
| 16 | *
|
|---|
| 17 | * It will only match incoming Requests whose
|
|---|
| 18 | * {@link https://fetch.spec.whatwg.org/#concept-request-mode|mode}
|
|---|
| 19 | * is set to `navigate`.
|
|---|
| 20 | *
|
|---|
| 21 | * You can optionally only apply this route to a subset of navigation requests
|
|---|
| 22 | * by using one or both of the `denylist` and `allowlist` parameters.
|
|---|
| 23 | *
|
|---|
| 24 | * @memberof workbox-routing
|
|---|
| 25 | * @extends workbox-routing.Route
|
|---|
| 26 | */
|
|---|
| 27 | class NavigationRoute extends Route {
|
|---|
| 28 | /**
|
|---|
| 29 | * If both `denylist` and `allowlist` are provided, the `denylist` will
|
|---|
| 30 | * take precedence and the request will not match this route.
|
|---|
| 31 | *
|
|---|
| 32 | * The regular expressions in `allowlist` and `denylist`
|
|---|
| 33 | * are matched against the concatenated
|
|---|
| 34 | * [`pathname`]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/pathname}
|
|---|
| 35 | * and [`search`]{@link https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/search}
|
|---|
| 36 | * portions of the requested URL.
|
|---|
| 37 | *
|
|---|
| 38 | * *Note*: These RegExps may be evaluated against every destination URL during
|
|---|
| 39 | * a navigation. Avoid using
|
|---|
| 40 | * [complex RegExps](https://github.com/GoogleChrome/workbox/issues/3077),
|
|---|
| 41 | * or else your users may see delays when navigating your site.
|
|---|
| 42 | *
|
|---|
| 43 | * @param {workbox-routing~handlerCallback} handler A callback
|
|---|
| 44 | * function that returns a Promise resulting in a Response.
|
|---|
| 45 | * @param {Object} options
|
|---|
| 46 | * @param {Array<RegExp>} [options.denylist] If any of these patterns match,
|
|---|
| 47 | * the route will not handle the request (even if a allowlist RegExp matches).
|
|---|
| 48 | * @param {Array<RegExp>} [options.allowlist=[/./]] If any of these patterns
|
|---|
| 49 | * match the URL's pathname and search parameter, the route will handle the
|
|---|
| 50 | * request (assuming the denylist doesn't match).
|
|---|
| 51 | */
|
|---|
| 52 | constructor(handler, { allowlist = [/./], denylist = [] } = {}) {
|
|---|
| 53 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 54 | assert.isArrayOfClass(allowlist, RegExp, {
|
|---|
| 55 | moduleName: 'workbox-routing',
|
|---|
| 56 | className: 'NavigationRoute',
|
|---|
| 57 | funcName: 'constructor',
|
|---|
| 58 | paramName: 'options.allowlist',
|
|---|
| 59 | });
|
|---|
| 60 | assert.isArrayOfClass(denylist, RegExp, {
|
|---|
| 61 | moduleName: 'workbox-routing',
|
|---|
| 62 | className: 'NavigationRoute',
|
|---|
| 63 | funcName: 'constructor',
|
|---|
| 64 | paramName: 'options.denylist',
|
|---|
| 65 | });
|
|---|
| 66 | }
|
|---|
| 67 | super((options) => this._match(options), handler);
|
|---|
| 68 | this._allowlist = allowlist;
|
|---|
| 69 | this._denylist = denylist;
|
|---|
| 70 | }
|
|---|
| 71 | /**
|
|---|
| 72 | * Routes match handler.
|
|---|
| 73 | *
|
|---|
| 74 | * @param {Object} options
|
|---|
| 75 | * @param {URL} options.url
|
|---|
| 76 | * @param {Request} options.request
|
|---|
| 77 | * @return {boolean}
|
|---|
| 78 | *
|
|---|
| 79 | * @private
|
|---|
| 80 | */
|
|---|
| 81 | _match({ url, request }) {
|
|---|
| 82 | if (request && request.mode !== 'navigate') {
|
|---|
| 83 | return false;
|
|---|
| 84 | }
|
|---|
| 85 | const pathnameAndSearch = url.pathname + url.search;
|
|---|
| 86 | for (const regExp of this._denylist) {
|
|---|
| 87 | if (regExp.test(pathnameAndSearch)) {
|
|---|
| 88 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 89 | logger.log(`The navigation route ${pathnameAndSearch} is not ` +
|
|---|
| 90 | `being used, since the URL matches this denylist pattern: ` +
|
|---|
| 91 | `${regExp.toString()}`);
|
|---|
| 92 | }
|
|---|
| 93 | return false;
|
|---|
| 94 | }
|
|---|
| 95 | }
|
|---|
| 96 | if (this._allowlist.some((regExp) => regExp.test(pathnameAndSearch))) {
|
|---|
| 97 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 98 | logger.debug(`The navigation route ${pathnameAndSearch} ` + `is being used.`);
|
|---|
| 99 | }
|
|---|
| 100 | return true;
|
|---|
| 101 | }
|
|---|
| 102 | if (process.env.NODE_ENV !== 'production') {
|
|---|
| 103 | logger.log(`The navigation route ${pathnameAndSearch} is not ` +
|
|---|
| 104 | `being used, since the URL being navigated to doesn't ` +
|
|---|
| 105 | `match the allowlist.`);
|
|---|
| 106 | }
|
|---|
| 107 | return false;
|
|---|
| 108 | }
|
|---|
| 109 | }
|
|---|
| 110 | export { NavigationRoute };
|
|---|