From 1edf77d42872c44b8009b94d01190358131fb222 Mon Sep 17 00:00:00 2001 From: Cy Klassen Date: Mon, 6 Mar 2017 09:54:27 -0800 Subject: [PATCH] Origin option can be regular expression (#47) * Origin option can be regular expression * Update readme with route origin option Regexp support --- README.md | 2 +- lib/service-worker.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 222090d..0fa921f 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ The following options allow you to specify routes that use [sw-toolbox's built-i ``` * **route** - the url or regular expression for the route. * **method** - the HTTP method for the route. Defaults to **any** which matches all HTTP methods. - * **options** - passed to the [route handler](https://github.com/GoogleChrome/sw-toolbox#methods) and are available for example to specify a different origin domain. + * **options** - passed to the [route handler](https://github.com/GoogleChrome/sw-toolbox#methods) and are available for example to specify a different origin domain (can use regular expression). ####Hooks The following hooks are available to your service worker code. Implement a hook by defining a `function` by the hook's name and it will be called. diff --git a/lib/service-worker.js b/lib/service-worker.js index 0a7555c..acd51d0 100644 --- a/lib/service-worker.js +++ b/lib/service-worker.js @@ -190,8 +190,13 @@ function setupRoutes(routeUrls, handler, lines) { routeUrls.forEach(function(cacheURL, idx, array) { var method = 'any'; var options; + var origin; if (typeof cacheURL === 'object' && (cacheURL instanceof RegExp === false)) { + origin = cacheURL.options && cacheURL.options.origin; + if (origin) { + cacheURL.options.origin = '[origin]'; + } options = JSON.stringify(cacheURL.options); if (cacheURL.method) { method = cacheURL.method; @@ -205,6 +210,10 @@ function setupRoutes(routeUrls, handler, lines) { line += options; } + if (origin) { + line = line.replace('"[origin]"', printPath(origin)); + } + line += ');'; lines.push(line);