source: frontend/node_modules/workbox-routing/src/NavigationRoute.ts

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

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