diff --git a/lib/error-factory.js b/lib/error-factory.js index 1c484f016..5dccc5685 100644 --- a/lib/error-factory.js +++ b/lib/error-factory.js @@ -37,7 +37,7 @@ class ErrorFactory { throw new Error(`Unable to generate error '${name}'.`); } - let message = this._errors[name]; + let message = this._errors[name].replace(/\s+/g, ' '); let stack = null; if (thrownError) { message += ` [${thrownError.message}]`; diff --git a/packages/sw-routing/src/lib/error-factory.js b/packages/sw-routing/src/lib/error-factory.js new file mode 100644 index 000000000..a2c1205ff --- /dev/null +++ b/packages/sw-routing/src/lib/error-factory.js @@ -0,0 +1,24 @@ +/* + Copyright 2016 Google Inc. All Rights Reserved. + 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 + + http://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 ErrorFactory from '../../../../lib/error-factory'; + +const errors = { + 'express-route-requires-absolute-path': `When using ExpressRoute, you must + provide a path that starts with a '/' character. You can only match + same-origin requests. For more flexibility, use RegExpRoute.`, +}; + +export default new ErrorFactory(errors); diff --git a/packages/sw-routing/src/lib/express-route.js b/packages/sw-routing/src/lib/express-route.js index f71325b39..4ce78b317 100644 --- a/packages/sw-routing/src/lib/express-route.js +++ b/packages/sw-routing/src/lib/express-route.js @@ -13,8 +13,8 @@ limitations under the License. */ +import ErrorFactory from './error-factory'; import Route from './route'; -import assert from '../../../../lib/assert'; import pathToRegExp from 'path-to-regexp'; /** @@ -63,7 +63,9 @@ class ExpressRoute extends Route { * HTTP method. Defaults to `'GET'` if not specified. */ constructor({path, handler, method}) { - assert.isType({path}, 'string'); + if (path.substring(0, 1) !== '/') { + throw ErrorFactory.createError('express-route-requires-absolute-path'); + } let keys = []; // keys is populated as a side effect of pathToRegExp. This isn't the nicest @@ -71,8 +73,12 @@ class ExpressRoute extends Route { // https://github.com/pillarjs/path-to-regexp#usage const regExp = pathToRegExp(path, keys); const match = ({url}) => { - const regexpMatches = url.pathname.match(regExp); + // Return null immediately if we have a cross-origin request. + if (url.origin !== location.origin) { + return null; + } + const regexpMatches = url.pathname.match(regExp); // Return null immediately if this route doesn't match. if (!regexpMatches) { return null;