Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v2: Warn on usage of Express-style wildcard characters #1049

Merged
merged 2 commits into from
Nov 16, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/workbox-sw/src/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

/* eslint-env browser, serviceworker */

import logHelper from '../../../../lib/log-helper';
import {
Router as SWRoutingRouter,
ExpressRoute,
Expand Down Expand Up @@ -101,6 +102,26 @@ class Router extends SWRoutingRouter {
if (capture.length === 0) {
throw ErrorFactory.createError('empty-express-string');
}
// See https://github.com/pillarjs/path-to-regexp#parameters
const wildcards = '[*:?+]';
const valueToCheck = capture.startsWith('http') ?
new URL(capture, location).pathname :
capture;
const match = valueToCheck.match(new RegExp(`${wildcards}`));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Change match to possibleExpressString

if (match) {
logHelper.warn({
message: `registerRoute() was called with a string containing an ` +
`Express-style wildcard character. While this is currently ` +
`supported, it will no longer be treated as a wildcard match in ` +

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplify the message here.

Suggestion:
registerRoute() was called with a string containing an express-style wild card character. In the next version of Workbox express style routes won't be supported by default, instead strings will have to be exact matches. Please switch to regulare expressions.

`an upcoming release of Workbox. For equivalent behavior, please ` +
`switch to using a regular expression instead.`,
data: {
'Path String': capture,
'Wildcard Character': match[0],
'Learn More': 'https://goo.gl/xZMKEV',
},
});
}
route = new ExpressRoute({path: capture, handler, method});
} else if (capture instanceof RegExp) {
route = new RegExpRoute({regExp: capture, handler, method});
Expand Down