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