Skip to content

Commit

Permalink
Origin option can be regular expression (#47)
Browse files Browse the repository at this point in the history
* Origin option can be regular expression

* Update readme with route origin option Regexp support
  • Loading branch information
cyk authored and jkleinsc committed Mar 6, 2017
1 parent bfd4db5 commit 1edf77d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions lib/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -205,6 +210,10 @@ function setupRoutes(routeUrls, handler, lines) {
line += options;
}

if (origin) {
line = line.replace('"[origin]"', printPath(origin));
}

line += ');';

lines.push(line);
Expand Down

0 comments on commit 1edf77d

Please sign in to comment.