1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {NavigationRoute} from './NavigationRoute.mjs';
import {RegExpRoute} from './RegExpRoute.mjs';
import {Router} from './Router.mjs';
import {Route} from './Route.mjs';
import {WorkboxError} from 'workbox-core/_private/WorkboxError.mjs';
import {assert} from 'workbox-core/_private/assert.mjs';
import {cacheNames} from 'workbox-core/_private/cacheNames.mjs';
import {logger} from 'workbox-core/_private/logger.mjs';
import './_version.mjs';
if (process.env.NODE_ENV !== 'production') {
assert.isSwEnv('workbox-routing');
}
/**
* @private
*/
class DefaultRouter extends Router {
/**
* Easily register a RegExp, string, or function with a caching
* strategy to the Router.
*
* This method will generate a Route for you if needed and
* call [Router.registerRoute()]{@link
* workbox.routing.Router#registerRoute}.
*
* @param {
* RegExp|
* string|
* workbox.routing.Route~matchCallback|
* workbox.routing.Route
* } capture
* If the capture param is a `Route`, all other arguments will be ignored.
* @param {workbox.routing.Route~handlerCallback} handler A callback
* function that returns a Promise resulting in a Response.
* @param {string} [method='GET'] The HTTP method to match the Route
* against.
* @return {workbox.routing.Route} The generated `Route`(Useful for
* unregistering).
*
* @alias workbox.routing.registerRoute
*/
registerRoute(capture, handler, method = 'GET') {
let route;
if (typeof capture === 'string') {
const captureUrl = new URL(capture, location);
if (process.env.NODE_ENV !== 'production') {
if (!(capture.startsWith('/') || capture.startsWith('http'))) {
throw new WorkboxError('invalid-string', {
moduleName: 'workbox-routing',
className: 'DefaultRouter',
funcName: 'registerRoute',
paramName: 'capture',
});
}
// We want to check if Express-style wildcards are in the pathname only.
// TODO: Remove this log message in v4.
const valueToCheck = capture.startsWith('http') ?
captureUrl.pathname :
capture;
// See https://github.com/pillarjs/path-to-regexp#parameters
const wildcards = '[*:?+]';
if (valueToCheck.match(new RegExp(`${wildcards}`))) {
logger.debug(
`The '$capture' parameter contains an Express-style wildcard ` +
`character (${wildcards}). Strings are now always interpreted as ` +
`exact matches; use a RegExp for partial or wildcard matches.`
);
}
}
const matchCallback = ({url}) => {
if (process.env.NODE_ENV !== 'production') {
if ((url.pathname === captureUrl.pathname) &&
(url.origin !== captureUrl.origin)) {
logger.debug(
`${capture} only partially matches the cross-origin URL ` +
`${url}. This route will only handle cross-origin requests ` +
`if they match the entire URL.`
);
}
}
return url.href === captureUrl.href;
};
route = new Route(matchCallback, handler, method);
} else if (capture instanceof RegExp) {
route = new RegExpRoute(capture, handler, method);
} else if (typeof capture === 'function') {
route = new Route(capture, handler, method);
} else if (capture instanceof Route) {
route = capture;
} else {
throw new WorkboxError('unsupported-route-type', {
moduleName: 'workbox-routing',
className: 'DefaultRouter',
funcName: 'registerRoute',
paramName: 'capture',
});
}
super.registerRoute(route);
return route;
}
/**
* Register a route that will return a precached file for a navigation
* request. This is useful for the
* [application shell pattern]{@link https://developers.google.com/web/fundamentals/architecture/app-shell}.
*
* This method will generate a
* [NavigationRoute]{@link workbox.routing.NavigationRoute}
* and call
* [Router.registerRoute()]{@link workbox.routing.Router#registerRoute}
* .
*
* @param {string} cachedAssetUrl
* @param {Object} [options]
* @param {string} [options.cacheName] Cache name to store and retrieve
* requests. Defaults to precache cache name provided by
* [workbox-core.cacheNames]{@link workbox.core.cacheNames}.
* @param {Array<RegExp>} [options.blacklist=[]] If any of these patterns
* match, the route will not handle the request (even if a whitelist entry
* matches).
* @param {Array<RegExp>} [options.whitelist=[/./]] If any of these patterns
* match the URL's pathname and search parameter, the route will handle the
* request (assuming the blacklist doesn't match).
* @return {workbox.routing.NavigationRoute} Returns the generated
* Route.
*
* @alias workbox.routing.registerNavigationRoute
*/
registerNavigationRoute(cachedAssetUrl, options = {}) {
if (process.env.NODE_ENV !== 'production') {
assert.isType(cachedAssetUrl, 'string', {
moduleName: 'workbox-routing',
className: '[default export]',
funcName: 'registerNavigationRoute',
paramName: 'cachedAssetUrl',
});
}
const cacheName = cacheNames.getPrecacheName(options.cacheName);
const handler = () => caches.match(cachedAssetUrl, {cacheName})
.then((response) => {
if (response) {
return response;
}
// This shouldn't normally happen, but there are edge cases:
// https://github.com/GoogleChrome/workbox/issues/1441
throw new Error(`The cache ${cacheName} did not have an entry for ` +
`${cachedAssetUrl}.`);
}).catch((error) => {
// If there's either a cache miss, or the caches.match() call threw
// an exception, then attempt to fulfill the navigation request with
// a response from the network rather than leaving the user with a
// failed navigation.
if (process.env.NODE_ENV !== 'production') {
logger.debug(`Unable to respond to navigation request with cached ` +
`response: ${error.message}. Falling back to network.`);
}
// This might still fail if the browser is offline...
return fetch(cachedAssetUrl);
});
const route = new NavigationRoute(handler, {
whitelist: options.whitelist,
blacklist: options.blacklist,
});
super.registerRoute(route);
return route;
}
}
const router = new DefaultRouter();
// By default, register a fetch event listener that will respond to a request
// only if there's a matching route.
self.addEventListener('fetch', (event) => {
const responsePromise = router.handleRequest(event);
if (responsePromise) {
event.respondWith(responsePromise);
}
});
export default router;
|