From b20bb452d07a7511f7a2bea1198b9fe9c7a1ff38 Mon Sep 17 00:00:00 2001 From: Jeff Posnick Date: Wed, 15 Nov 2017 09:54:09 -0500 Subject: [PATCH 1/2] Warn on usage of Express-style wildcard characters. --- packages/workbox-sw/src/lib/router.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/workbox-sw/src/lib/router.js b/packages/workbox-sw/src/lib/router.js index b5ecd6836..289343906 100644 --- a/packages/workbox-sw/src/lib/router.js +++ b/packages/workbox-sw/src/lib/router.js @@ -15,6 +15,7 @@ /* eslint-env browser, serviceworker */ +import logHelper from '../../../../lib/log-helper'; import { Router as SWRoutingRouter, ExpressRoute, @@ -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}`)); + 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 ` + + `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}); From dcb7d55d7189ef250b2836c5452c39d920189cc0 Mon Sep 17 00:00:00 2001 From: Jeff Posnick Date: Thu, 16 Nov 2017 16:09:08 -0500 Subject: [PATCH 2/2] Review feedback. --- packages/workbox-sw/src/lib/router.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/workbox-sw/src/lib/router.js b/packages/workbox-sw/src/lib/router.js index 289343906..6ba991559 100644 --- a/packages/workbox-sw/src/lib/router.js +++ b/packages/workbox-sw/src/lib/router.js @@ -107,17 +107,18 @@ class Router extends SWRoutingRouter { const valueToCheck = capture.startsWith('http') ? new URL(capture, location).pathname : capture; - const match = valueToCheck.match(new RegExp(`${wildcards}`)); - if (match) { + const possibleExpressString = valueToCheck.match( + new RegExp(`${wildcards}`)); + if (possibleExpressString) { 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 ` + - `an upcoming release of Workbox. For equivalent behavior, please ` + - `switch to using a regular expression instead.`, + `Express-style wildcard character. In the next version of `+ + `Workbox, Express-style wildcards won't be supported, and ` + + `strings will be treated a exact matches. Please switch to ` + + `regular expressions for equivalent behavior.`, data: { 'Path String': capture, - 'Wildcard Character': match[0], + 'Wildcard Character': possibleExpressString[0], 'Learn More': 'https://goo.gl/xZMKEV', }, });