Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #49 from PolymerElements/various-fixes
Browse files Browse the repository at this point in the history
Various docs/developer experience fixes
  • Loading branch information
jeffposnick committed Jun 30, 2015
2 parents faf7d88 + 876de38 commit 49d4898
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "platinum-sw",
"private": true,
"version": "1.0.1",
"version": "1.0.2",
"license": "http://polymer.github.io/LICENSE.txt",
"authors": [
"The Polymer Authors"
Expand Down
5 changes: 3 additions & 2 deletions platinum-sw-cache.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
* <platinum-sw-cache></platinum-sw-cache>
* </platinum-sw-register>
*
* This is enough to have all of the (local) resources your site uses cached at runtime.
* This is enough to have all of the resources your site uses cached at runtime, both local and
* cross-origin.
* (It uses the default `defaultCacheStrategy` of "networkFirst".)
* When there's a network available, visits to your site will go against the network copy of the
* resources, but if someone visits your site when they're offline, all the cached resources will
Expand All @@ -34,7 +35,7 @@

properties: {
/**
* The caching strategy used for all local (i.e. same-origin) requests.
* The caching strategy used for all requests, both for local and cross-origin resources.
*
* For a list of strategies, see the [`sw-toolbox` documentation](https://github.com/GoogleChrome/sw-toolbox#built-in-handlers).
* Specify a strategy as a string, without the "toolbox" prefix. E.g., for
Expand Down
16 changes: 11 additions & 5 deletions platinum-sw-fetch.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
* and it's possible to have multiple `<platinum-sw-fetch>` elements that each define different
* paths and different handlers. The path matching is performed top-down, starting with the first
* `<platinum-sw-fetch>` element.
*
* The `path` will, by default, only match same-origin requests. If you'd like to define a custom
* handler for requests on a specific cross-origin domain, you must use the `origin` parameter
* in conjunction with `path` to match the domains you'd like to handle.
*/
Polymer({
is: 'platinum-sw-fetch',
Expand Down Expand Up @@ -75,16 +79,15 @@
*
* `origin` is a `String`, but it is used internally to construct a
* [`RegExp` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions),
* which is used for the matching. To properly escape RegExp sequences like `\.`, it's
* necessary to add in an extra level of string-escaping first ('\\.').
* which is used for the matching.
*
* Note that the `origin` value will be matched against the full domain name and the protocol.
* If you want to match 'http' and 'https', then use 'https?://' at the start of your string.
*
* Some examples:
* - `origin="https?://.+\\.google\\.com"` → a RegExp that matches `http` or `https` requests
* made to any domain, as long as it ends in `.google.com`.
* - `origin="https://www\\.example\\.com" → a RegExp that will only match `https` requests to
* - `origin="https?://.+\.google\.com"` → a RegExp that matches `http` or `https` requests
* made to any domain that ends in `.google.com`.
* - `origin="https://www\.example\.com" → a RegExp that will only match `https` requests to
* one domain, `www.example.com`.
*
* @see {@link https://github.com/googlechrome/sw-toolbox#toolboxrouterheadurlpattern-handler-options}
Expand Down Expand Up @@ -122,6 +125,9 @@
};
if (this.path && this.handler) {
params.route = [this.path, this.handler, this.origin];
} else {
console.warn('The following platinum-sw-fetch element will not have any effect. ' +
'Both the "path" and "handler" attributes must be set.', this);
}
resolve(params);
}.bind(this));
Expand Down
3 changes: 3 additions & 0 deletions platinum-sw-import-script.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
var params = {};
if (this.href) {
params.importscript = this.href;
} else {
console.warn('The following platinum-sw-import-script element will not have any effect.' +
' The "href" attribute must be set.', this);
}
resolve(params);
}.bind(this));
Expand Down
2 changes: 1 addition & 1 deletion test/platinum-sw-cache/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<body>
<platinum-sw-register skip-waiting clients-claim auto-register>
<platinum-sw-cache defaultCacheStrategy="networkOnly"
<platinum-sw-cache default-cache-strategy="networkOnly"
precache='["file.html", "file.txt"]'
precache-file="files.json"></platinum-sw-cache>
</platinum-sw-register>
Expand Down
9 changes: 8 additions & 1 deletion test/platinum-sw-fetch/custom-fetch-handler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
self.customFetchHandler = function(request) {
self.custom204FetchHandler = function(request) {
return new Response('', {
status: 204,
statusText: 'Via customFetchHandler'
});
};

self.custom410FetchHandler = function(request) {
return new Response('', {
status: 410,
statusText: 'Via customFetchHandler'
});
};
19 changes: 15 additions & 4 deletions test/platinum-sw-fetch/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,39 @@

<body>
<platinum-sw-register skip-waiting clients-claim auto-register>
<platinum-sw-fetch handler="customFetchHandler"
path="/(.*)/customFetch"></platinum-sw-fetch>
<platinum-sw-fetch handler="custom204FetchHandler"
path="/(.*)customFetch"></platinum-sw-fetch>
<platinum-sw-fetch handler="custom410FetchHandler"
path="/(.*)customFetch"
origin="https://matching\.domain"></platinum-sw-fetch>
<platinum-sw-import-script href="custom-fetch-handler.js"></platinum-sw-import-script>
</platinum-sw-register>

<script>
suite('Service Worker Fetch Handlers', function() {
test('the custom fetch handler is used when the path matches', function() {
test('the same-origin custom fetch handler is used when the path matches', function() {
return navigator.serviceWorker.ready.then(function() {
return window.fetch('customFetch').then(function(response) {
assert.equal(response.status, 204, 'Custom response status doesn\'t match');
});
});
});

test('the custom fetch handler isn\'t used when the path doesn\t match', function() {
test('the same-origin custom fetch handler isn\'t used when the path doesn\t match', function() {
return navigator.serviceWorker.ready.then(function() {
return window.fetch('dummyUrlThatShould404').then(function(response) {
assert.equal(response.status, 404, 'Expected response status doesn\'t match');
});
});
});

test('the cross-origin fetch handler is used when the path and origin matches', function() {
return navigator.serviceWorker.ready.then(function() {
return window.fetch('https://matching.domain/path/to/customFetch').then(function(response) {
assert.equal(response.status, 410, 'Custom response status doesn\'t match');
});
});
});
});
</script>
</body>
Expand Down

0 comments on commit 49d4898

Please sign in to comment.