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

Origin option can be regular expression #47

Merged
merged 2 commits into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,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 @@ -188,8 +188,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 @@ -203,6 +208,10 @@ function setupRoutes(routeUrls, handler, lines) {
line += options;
}

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

line += ');';

lines.push(line);
Expand Down