source: frontend/node_modules/workbox-routing/registerRoute.js

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

Fix frontend appearance

  • Property mode set to 100644
File size: 3.9 KB
Line 
1/*
2 Copyright 2019 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*/
8import { logger } from 'workbox-core/_private/logger.js';
9import { WorkboxError } from 'workbox-core/_private/WorkboxError.js';
10import { Route } from './Route.js';
11import { RegExpRoute } from './RegExpRoute.js';
12import { getOrCreateDefaultRouter } from './utils/getOrCreateDefaultRouter.js';
13import './_version.js';
14/**
15 * Easily register a RegExp, string, or function with a caching
16 * strategy to a singleton Router instance.
17 *
18 * This method will generate a Route for you if needed and
19 * call {@link workbox-routing.Router#registerRoute}.
20 *
21 * @param {RegExp|string|workbox-routing.Route~matchCallback|workbox-routing.Route} capture
22 * If the capture param is a `Route`, all other arguments will be ignored.
23 * @param {workbox-routing~handlerCallback} [handler] A callback
24 * function that returns a Promise resulting in a Response. This parameter
25 * is required if `capture` is not a `Route` object.
26 * @param {string} [method='GET'] The HTTP method to match the Route
27 * against.
28 * @return {workbox-routing.Route} The generated `Route`.
29 *
30 * @memberof workbox-routing
31 */
32function registerRoute(capture, handler, method) {
33 let route;
34 if (typeof capture === 'string') {
35 const captureUrl = new URL(capture, location.href);
36 if (process.env.NODE_ENV !== 'production') {
37 if (!(capture.startsWith('/') || capture.startsWith('http'))) {
38 throw new WorkboxError('invalid-string', {
39 moduleName: 'workbox-routing',
40 funcName: 'registerRoute',
41 paramName: 'capture',
42 });
43 }
44 // We want to check if Express-style wildcards are in the pathname only.
45 // TODO: Remove this log message in v4.
46 const valueToCheck = capture.startsWith('http')
47 ? captureUrl.pathname
48 : capture;
49 // See https://github.com/pillarjs/path-to-regexp#parameters
50 const wildcards = '[*:?+]';
51 if (new RegExp(`${wildcards}`).exec(valueToCheck)) {
52 logger.debug(`The '$capture' parameter contains an Express-style wildcard ` +
53 `character (${wildcards}). Strings are now always interpreted as ` +
54 `exact matches; use a RegExp for partial or wildcard matches.`);
55 }
56 }
57 const matchCallback = ({ url }) => {
58 if (process.env.NODE_ENV !== 'production') {
59 if (url.pathname === captureUrl.pathname &&
60 url.origin !== captureUrl.origin) {
61 logger.debug(`${capture} only partially matches the cross-origin URL ` +
62 `${url.toString()}. This route will only handle cross-origin requests ` +
63 `if they match the entire URL.`);
64 }
65 }
66 return url.href === captureUrl.href;
67 };
68 // If `capture` is a string then `handler` and `method` must be present.
69 route = new Route(matchCallback, handler, method);
70 }
71 else if (capture instanceof RegExp) {
72 // If `capture` is a `RegExp` then `handler` and `method` must be present.
73 route = new RegExpRoute(capture, handler, method);
74 }
75 else if (typeof capture === 'function') {
76 // If `capture` is a function then `handler` and `method` must be present.
77 route = new Route(capture, handler, method);
78 }
79 else if (capture instanceof Route) {
80 route = capture;
81 }
82 else {
83 throw new WorkboxError('unsupported-route-type', {
84 moduleName: 'workbox-routing',
85 funcName: 'registerRoute',
86 paramName: 'capture',
87 });
88 }
89 const defaultRouter = getOrCreateDefaultRouter();
90 defaultRouter.registerRoute(route);
91 return route;
92}
93export { registerRoute };
Note: See TracBrowser for help on using the repository browser.