polymer-platinum-rails 1.0.0.pre.rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +39 -0
- data/Rakefile +1 -0
- data/app/assets/components/platinum-elements/LICENSE +202 -0
- data/app/assets/components/platinum-elements/README.md +2 -0
- data/app/assets/components/platinum-elements/bower.json +19 -0
- data/app/assets/components/platinum-push-messaging/README.md +4 -0
- data/app/assets/components/platinum-push-messaging/bower.json +37 -0
- data/app/assets/components/platinum-push-messaging/demo/index.html +130 -0
- data/app/assets/components/platinum-push-messaging/demo/manifest.json +4 -0
- data/app/assets/components/platinum-push-messaging/index.html +22 -0
- data/app/assets/components/platinum-push-messaging/platinum-push-messaging.html +427 -0
- data/app/assets/components/platinum-push-messaging/service-worker.js +151 -0
- data/app/assets/components/platinum-sw/README.md +43 -0
- data/app/assets/components/platinum-sw/bootstrap/sw-toolbox-setup.js +38 -0
- data/app/assets/components/platinum-sw/bower.json +37 -0
- data/app/assets/components/platinum-sw/demo/index.html +91 -0
- data/app/assets/components/platinum-sw/demo/sw-import.js +1 -0
- data/app/assets/components/platinum-sw/index.html +22 -0
- data/app/assets/components/platinum-sw/platinum-sw-cache.html +116 -0
- data/app/assets/components/platinum-sw/platinum-sw-elements.html +14 -0
- data/app/assets/components/platinum-sw/platinum-sw-fetch.html +130 -0
- data/app/assets/components/platinum-sw/platinum-sw-import-script.html +57 -0
- data/app/assets/components/platinum-sw/platinum-sw-register.html +342 -0
- data/app/assets/components/platinum-sw/service-worker.js +52 -0
- data/app/assets/components/polymer/LICENSE.txt +27 -0
- data/app/assets/components/polymer/bower.json +26 -0
- data/app/assets/components/polymer/build.log +27 -0
- data/app/assets/components/polymer/polymer-micro.html +523 -0
- data/app/assets/components/polymer/polymer-mini.html +1368 -0
- data/app/assets/components/polymer/polymer.html +3768 -0
- data/app/assets/components/sw-toolbox/CONTRIBUTING.md +30 -0
- data/app/assets/components/sw-toolbox/LICENSE +201 -0
- data/app/assets/components/sw-toolbox/README.md +156 -0
- data/app/assets/components/sw-toolbox/bower.json +14 -0
- data/app/assets/components/sw-toolbox/companion.js +23 -0
- data/app/assets/components/sw-toolbox/package.json +21 -0
- data/app/assets/components/sw-toolbox/sw-toolbox.js +148 -0
- data/app/assets/components/sw-toolbox/sw-toolbox.map.json +1 -0
- data/app/assets/components/webcomponentsjs/CustomElements.js +956 -0
- data/app/assets/components/webcomponentsjs/CustomElements.min.js +11 -0
- data/app/assets/components/webcomponentsjs/HTMLImports.js +1078 -0
- data/app/assets/components/webcomponentsjs/HTMLImports.min.js +11 -0
- data/app/assets/components/webcomponentsjs/MutationObserver.js +344 -0
- data/app/assets/components/webcomponentsjs/MutationObserver.min.js +11 -0
- data/app/assets/components/webcomponentsjs/README.md +125 -0
- data/app/assets/components/webcomponentsjs/ShadowDOM.js +4414 -0
- data/app/assets/components/webcomponentsjs/ShadowDOM.min.js +15 -0
- data/app/assets/components/webcomponentsjs/bower.json +14 -0
- data/app/assets/components/webcomponentsjs/build.log +33 -0
- data/app/assets/components/webcomponentsjs/package.json +31 -0
- data/app/assets/components/webcomponentsjs/webcomponents-lite.js +2300 -0
- data/app/assets/components/webcomponentsjs/webcomponents-lite.min.js +13 -0
- data/app/assets/components/webcomponentsjs/webcomponents.js +7112 -0
- data/app/assets/components/webcomponentsjs/webcomponents.min.js +15 -0
- data/lib/polymer-platinum-rails.rb +2 -0
- data/lib/polymer-platinum-rails/engine.rb +4 -0
- data/lib/polymer-platinum-rails/version.rb +3 -0
- metadata +149 -0
@@ -0,0 +1,151 @@
|
|
1
|
+
/*
|
2
|
+
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
|
3
|
+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
|
4
|
+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
|
5
|
+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
|
6
|
+
Code distributed by Google as part of the polymer project is also
|
7
|
+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
|
8
|
+
*/
|
9
|
+
'use strict';
|
10
|
+
|
11
|
+
var options = JSON.parse(decodeURIComponent(location.search.substring(1)));
|
12
|
+
|
13
|
+
var DEFAULT_TAG = self.registration.scope;
|
14
|
+
var DATA_SUPPORT = Notification.prototype.hasOwnProperty('data');
|
15
|
+
|
16
|
+
self.skipWaiting();
|
17
|
+
|
18
|
+
/**
|
19
|
+
* Resolves a URL that is relative to the registering page to an absolute URL
|
20
|
+
*
|
21
|
+
* @param url {String} a relative URL
|
22
|
+
* @return {String} the equivalent absolute URL
|
23
|
+
*/
|
24
|
+
var absUrl = function(url) {
|
25
|
+
if (typeof(url) === 'string') {
|
26
|
+
return new URL(url, options.baseUrl).href;
|
27
|
+
}
|
28
|
+
};
|
29
|
+
|
30
|
+
var getClientWindows = function() {
|
31
|
+
return clients.matchAll({
|
32
|
+
type: 'window',
|
33
|
+
includeUncontrolled: true
|
34
|
+
}).catch(function(error) {
|
35
|
+
// Couldn't get client list, possibly not yet implemented in the browser
|
36
|
+
return [];
|
37
|
+
});
|
38
|
+
};
|
39
|
+
|
40
|
+
var getVisible = function(url) {
|
41
|
+
return getClientWindows().then(function(clientList) {
|
42
|
+
for (var client of clientList) {
|
43
|
+
if (client.url === url && client.focused &&
|
44
|
+
client.visibilityState === 'visible') {
|
45
|
+
return client;
|
46
|
+
}
|
47
|
+
}
|
48
|
+
return null;
|
49
|
+
});
|
50
|
+
};
|
51
|
+
|
52
|
+
var messageClient = function(client, message, notificationShown) {
|
53
|
+
client.postMessage({
|
54
|
+
source: self.registration.scope,
|
55
|
+
message: message,
|
56
|
+
type: notificationShown ? 'click' : 'push'
|
57
|
+
});
|
58
|
+
};
|
59
|
+
|
60
|
+
var notify = function(data) {
|
61
|
+
var messagePromise;
|
62
|
+
|
63
|
+
if (options.messageUrl) {
|
64
|
+
messagePromise = fetch(absUrl(options.messageUrl)).then(function(response) {
|
65
|
+
return response.json();
|
66
|
+
});
|
67
|
+
} else {
|
68
|
+
messagePromise = data ? data.json() : Promise.resolve({});
|
69
|
+
}
|
70
|
+
|
71
|
+
return messagePromise.then(function(message) {
|
72
|
+
var detail = {
|
73
|
+
title: message.title || options.title || '',
|
74
|
+
body: message.message || options.message || '',
|
75
|
+
tag: message.tag || options.tag || DEFAULT_TAG,
|
76
|
+
icon: absUrl(message.icon || options.iconUrl),
|
77
|
+
data: message
|
78
|
+
};
|
79
|
+
|
80
|
+
var clickUrl = absUrl(message.url || options.clickUrl);
|
81
|
+
|
82
|
+
if (!DATA_SUPPORT) {
|
83
|
+
// If there is no 'data' property support on the notification then we have
|
84
|
+
// to pass the link URL (and anything else) some other way. We use the
|
85
|
+
// hash of the icon URL to store it.
|
86
|
+
var iconUrl = new URL(detail.icon || 'about:blank');
|
87
|
+
iconUrl.hash = encodeURIComponent(JSON.stringify(detail.data));
|
88
|
+
detail.icon = iconUrl.href;
|
89
|
+
}
|
90
|
+
|
91
|
+
return getVisible(clickUrl).then(function(visibleClient) {
|
92
|
+
if (visibleClient) {
|
93
|
+
messageClient(visibleClient, message, false);
|
94
|
+
} else {
|
95
|
+
return self.registration.showNotification(detail.title, detail);
|
96
|
+
}
|
97
|
+
});
|
98
|
+
});
|
99
|
+
};
|
100
|
+
|
101
|
+
var clickHandler = function(notification) {
|
102
|
+
notification.close();
|
103
|
+
|
104
|
+
var message;
|
105
|
+
if ('data' in notification) {
|
106
|
+
message = notification.data;
|
107
|
+
} else {
|
108
|
+
message = new URL(notification.icon).hash.substring(1);
|
109
|
+
message = JSON.parse(decodeURIComponent(message));
|
110
|
+
}
|
111
|
+
|
112
|
+
var url = absUrl(message.url || options.clickUrl);
|
113
|
+
|
114
|
+
if (!url) {
|
115
|
+
return;
|
116
|
+
}
|
117
|
+
|
118
|
+
return getClientWindows().then(function(clientList) {
|
119
|
+
for (var client of clientList) {
|
120
|
+
if (client.url === url && 'focus' in client) {
|
121
|
+
client.focus();
|
122
|
+
return client;
|
123
|
+
}
|
124
|
+
}
|
125
|
+
if ('openWindow' in clients) {
|
126
|
+
return clients.openWindow(url);
|
127
|
+
}
|
128
|
+
}).then(function(client) {
|
129
|
+
if (client) {
|
130
|
+
messageClient(client, message, true);
|
131
|
+
}
|
132
|
+
});
|
133
|
+
};
|
134
|
+
|
135
|
+
self.addEventListener('push', function(event) {
|
136
|
+
event.waitUntil(notify(event.data));
|
137
|
+
});
|
138
|
+
|
139
|
+
self.addEventListener('notificationclick', function(event) {
|
140
|
+
event.waitUntil(clickHandler(event.notification));
|
141
|
+
});
|
142
|
+
|
143
|
+
self.addEventListener('message', function(event) {
|
144
|
+
if (event.data.type == 'test-push') {
|
145
|
+
notify({
|
146
|
+
json: function() {
|
147
|
+
return Promise.resolve(event.data.message);
|
148
|
+
}
|
149
|
+
});
|
150
|
+
}
|
151
|
+
});
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# Platinum Service Worker Elements
|
2
|
+
A set of Polymer elements that simplify service worker registration and caching, powered by the
|
3
|
+
[`sw-toolbox` library](https://github.com/googlechrome/sw-toolbox).
|
4
|
+
Full documentation is available at https://PolymerElements.github.io/platinum-sw/index.html
|
5
|
+
|
6
|
+
# Considerations
|
7
|
+
|
8
|
+
## Top-level `sw-import.js`
|
9
|
+
While `<platinum-sw-register>` abstracts away many of the details of working with service workers,
|
10
|
+
there is one specific requirement that developers must fulfill: it needs to register a JavaScript file
|
11
|
+
located at the top-level of your site's web root. (Details behind this requirement can be found in
|
12
|
+
the service worker specification [issue tracker](https://github.com/slightlyoff/ServiceWorker/issues/468#issuecomment-60276779).)
|
13
|
+
|
14
|
+
In order to use `<platinum-sw-register>`, it's recommended that you create a `sw-import.js` file in
|
15
|
+
your site's web root. The file's only contents should be
|
16
|
+
|
17
|
+
importScripts('bower_components/platinum-sw/service-worker.js');
|
18
|
+
|
19
|
+
You can adjust the path to `service-worker.js` if your project has its Polymer elements
|
20
|
+
installed somewhere other than `bower_components/`.
|
21
|
+
|
22
|
+
If you have multiple subdirectories worth of pages on your site, it's recommend that you include the
|
23
|
+
`<platinum-sw-register>` element on a top-level entry page that all visitors will access first; once
|
24
|
+
they visit the top-level page and the service worker is registered, it will automatically apply to
|
25
|
+
all sub-pages, which will fall under its
|
26
|
+
[scope](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-registration-scope).
|
27
|
+
|
28
|
+
## `cacheOnly` & `cacheFirst` `defaultCacheStrategy` Considered Harmful
|
29
|
+
The [`sw-toolbox` library](https://github.com/googlechrome/sw-toolbox),
|
30
|
+
which `<platinum-sw-cache>` is built on, supports a number of
|
31
|
+
[caching strategies](https://github.com/googlechrome/sw-toolbox#built-in-handlers).
|
32
|
+
Two of them, `cacheOnly` and `cacheFirst`, are strongly discouraged to be used as the `defaultCacheStrategy`
|
33
|
+
for `<platinum-sw-cache>`. With both of those strategies, all HTTP requests, including requests for
|
34
|
+
the page which contains the `<platinum-sw-cache>` element, are served directly from the [Cache Storage
|
35
|
+
API](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#cache-objects) without
|
36
|
+
first consulting the network for a fresh copy. Once the copy of the host page is cached,
|
37
|
+
it's extremely difficult to change the configuration of the service worker (since the configuration
|
38
|
+
depends on the page's contents), and developers could find themselves deploying sites that can never
|
39
|
+
update.
|
40
|
+
|
41
|
+
In a future release of `<platinum-sw-cache>`, using `cacheOnly` and `cacheFirst` as `defaultCacheStrategy`
|
42
|
+
may lead to an explicit error condition, but for the meantime, please consider a more reasonable default
|
43
|
+
(like `networkFirst`).
|
@@ -0,0 +1,38 @@
|
|
1
|
+
/**
|
2
|
+
* @license
|
3
|
+
* Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
|
4
|
+
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
|
5
|
+
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
|
6
|
+
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
|
7
|
+
* Code distributed by Google as part of the polymer project is also
|
8
|
+
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
|
9
|
+
*/
|
10
|
+
|
11
|
+
var swToolboxURL = new URL('../sw-toolbox/sw-toolbox.js', params.get('baseURI')).href;
|
12
|
+
importScripts(swToolboxURL);
|
13
|
+
|
14
|
+
if (params.has('defaultCacheStrategy')) {
|
15
|
+
var strategy = params.get('defaultCacheStrategy');
|
16
|
+
toolbox.router.default = toolbox[strategy] || self[strategy];
|
17
|
+
}
|
18
|
+
|
19
|
+
if (params.has('precache')) {
|
20
|
+
toolbox.precache(params.get('precache'));
|
21
|
+
}
|
22
|
+
|
23
|
+
if (params.has('route')) {
|
24
|
+
var setsOfRouteParams = params.get('route');
|
25
|
+
while (setsOfRouteParams.length > 0) {
|
26
|
+
var routeParams = setsOfRouteParams.splice(0, 3);
|
27
|
+
var originParam;
|
28
|
+
if (routeParams[2]) {
|
29
|
+
originParam = {origin: new RegExp(routeParams[2])};
|
30
|
+
}
|
31
|
+
var handler = toolbox[routeParams[1]] || self[routeParams[1]];
|
32
|
+
if (typeof handler === 'function') {
|
33
|
+
toolbox.router.get(routeParams[0], handler, originParam);
|
34
|
+
} else {
|
35
|
+
console.error('Unable to register sw-toolbox route: ', routeParams);
|
36
|
+
}
|
37
|
+
}
|
38
|
+
}
|
@@ -0,0 +1,37 @@
|
|
1
|
+
{
|
2
|
+
"name": "platinum-sw",
|
3
|
+
"private": true,
|
4
|
+
"version": "1.0.1",
|
5
|
+
"license": "http://polymer.github.io/LICENSE.txt",
|
6
|
+
"authors": [
|
7
|
+
"The Polymer Authors"
|
8
|
+
],
|
9
|
+
"description": "Service worker helper elements.",
|
10
|
+
"main": "platinum-sw-elements.html",
|
11
|
+
"keywords": [
|
12
|
+
"caching",
|
13
|
+
"offline",
|
14
|
+
"polymer",
|
15
|
+
"service-worker",
|
16
|
+
"serviceworker",
|
17
|
+
"web-component",
|
18
|
+
"web-components"
|
19
|
+
],
|
20
|
+
"ignore": [
|
21
|
+
"**/.*",
|
22
|
+
"node_modules",
|
23
|
+
"bower_components",
|
24
|
+
"test",
|
25
|
+
"tests"
|
26
|
+
],
|
27
|
+
"dependencies": {
|
28
|
+
"polymer": "Polymer/polymer#^1.0.0",
|
29
|
+
"sw-toolbox": "^2.0.2"
|
30
|
+
},
|
31
|
+
"devDependencies": {
|
32
|
+
"iron-component-page": "PolymerElements/iron-component-page#^1.0.2",
|
33
|
+
"marked-element": "PolymerElements/marked-element#^1.0.0",
|
34
|
+
"web-component-tester": "*",
|
35
|
+
"fetch": "^0.9.0"
|
36
|
+
}
|
37
|
+
}
|
@@ -0,0 +1,91 @@
|
|
1
|
+
<!doctype html>
|
2
|
+
<!--
|
3
|
+
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
|
4
|
+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
|
5
|
+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
|
6
|
+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
|
7
|
+
Code distributed by Google as part of the polymer project is also
|
8
|
+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
|
9
|
+
-->
|
10
|
+
<html>
|
11
|
+
<head>
|
12
|
+
<meta charset="utf-8">
|
13
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
14
|
+
<title>Platinum Service Worker Elements Demo</title>
|
15
|
+
|
16
|
+
<script src="../../fetch/fetch.js"></script>
|
17
|
+
<script src="../../webcomponentsjs/webcomponents-lite.min.js"></script>
|
18
|
+
<link rel="import" href="../platinum-sw-elements.html">
|
19
|
+
<link rel="import" href="../../marked-element/marked-element.html">
|
20
|
+
</head>
|
21
|
+
|
22
|
+
<body>
|
23
|
+
<template is="dom-bind" id="page-template">
|
24
|
+
<platinum-sw-register skip-waiting
|
25
|
+
clients-claim
|
26
|
+
reload-on-install
|
27
|
+
state="{{state}}">
|
28
|
+
<platinum-sw-cache default-cache-strategy="networkFirst"
|
29
|
+
precache="{{precacheList}}"></platinum-sw-cache>
|
30
|
+
</platinum-sw-register>
|
31
|
+
|
32
|
+
<h1>Platinum Service Worker Elements Demo</h1>
|
33
|
+
<p>This is a simple offline-capable eBook reader.</p>
|
34
|
+
<p>
|
35
|
+
On browsers that support service workers, this page itself and all the books are all
|
36
|
+
available offline, by virtue of the <code><platinum-sw-register></code> and
|
37
|
+
<code><platinum-sw-cache></code> elements.
|
38
|
+
</p>
|
39
|
+
<p>
|
40
|
+
Service workers are meant to be a progressive enhancement, and browsers that lack service
|
41
|
+
worker support will still have a functional (online-only) eBook reader.
|
42
|
+
</p>
|
43
|
+
|
44
|
+
<template is="dom-if" if="[[state]]">
|
45
|
+
<select on-change="selectBook">
|
46
|
+
<option disabled selected>Select a Book...</option>
|
47
|
+
<template is="dom-repeat" id="books" items="[[books]]">
|
48
|
+
<option>{{item.title}}</option>
|
49
|
+
</template>
|
50
|
+
</select>
|
51
|
+
</template>
|
52
|
+
|
53
|
+
<marked-element markdown="{{text}}"></marked-element>
|
54
|
+
</template>
|
55
|
+
|
56
|
+
<script>
|
57
|
+
var t = document.querySelector('#page-template');
|
58
|
+
|
59
|
+
t.books = [{
|
60
|
+
title: 'Don Quixote',
|
61
|
+
url: 'https://cdn.rawgit.com/GITenberg/Don-Quixote_996/master/996.txt'
|
62
|
+
}, {
|
63
|
+
title: 'Dubliners',
|
64
|
+
url: 'https://cdn.rawgit.com/GITenberg/Dubliners_2814/master/2814.txt'
|
65
|
+
}, {
|
66
|
+
title: 'Pride & Prejudice',
|
67
|
+
url: 'https://cdn.rawgit.com/GITenberg/Pride-and-Prejudice_1342/master/1342.txt'
|
68
|
+
}];
|
69
|
+
|
70
|
+
t.precacheList = t.books.map(function(book) {
|
71
|
+
return book.url;
|
72
|
+
});
|
73
|
+
|
74
|
+
t.selectBook = function(e) {
|
75
|
+
var books = document.querySelector('#books');
|
76
|
+
var selectedBook = books.itemForElement(e.target.selectedOptions[0]);
|
77
|
+
window.fetch(selectedBook.url).then(function(response) {
|
78
|
+
return response.text();
|
79
|
+
}).then(function(text) {
|
80
|
+
t.text = text;
|
81
|
+
});
|
82
|
+
};
|
83
|
+
|
84
|
+
window.addEventListener('WebComponentsReady', function() {
|
85
|
+
// Explicitly call the register() method. We need to wait until the template's variables are
|
86
|
+
// all set first, since the configuration depends on bound variables.
|
87
|
+
document.querySelector('platinum-sw-register').register();
|
88
|
+
});
|
89
|
+
</script>
|
90
|
+
</body>
|
91
|
+
</html>
|
@@ -0,0 +1 @@
|
|
1
|
+
importScripts('../service-worker.js');
|
@@ -0,0 +1,22 @@
|
|
1
|
+
<!doctype html>
|
2
|
+
<!--
|
3
|
+
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
|
4
|
+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE
|
5
|
+
The complete set of authors may be found at http://polymer.github.io/AUTHORS
|
6
|
+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS
|
7
|
+
Code distributed by Google as part of the polymer project is also
|
8
|
+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS
|
9
|
+
-->
|
10
|
+
<html>
|
11
|
+
<head>
|
12
|
+
<meta charset="utf-8">
|
13
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
14
|
+
|
15
|
+
<script src="../webcomponentsjs/webcomponents-lite.js"></script>
|
16
|
+
<link rel="import" href="../iron-component-page/iron-component-page.html">
|
17
|
+
</head>
|
18
|
+
|
19
|
+
<body>
|
20
|
+
<iron-component-page src="platinum-sw-elements.html"></iron-component-page>
|
21
|
+
</body>
|
22
|
+
</html>
|
@@ -0,0 +1,116 @@
|
|
1
|
+
<!--
|
2
|
+
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
|
3
|
+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
|
4
|
+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
|
5
|
+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
|
6
|
+
Code distributed by Google as part of the polymer project is also
|
7
|
+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
|
8
|
+
-->
|
9
|
+
<link rel="import" href="../polymer/polymer.html">
|
10
|
+
|
11
|
+
<script>
|
12
|
+
/**
|
13
|
+
* The `<platinum-sw-cache>` element makes it easy to precache specific resources, perform runtime
|
14
|
+
* caching, and serve your cached resources when a network is unavailable.
|
15
|
+
* Under the hood, the [sw-toolbox](https://github.com/googlechrome/sw-toolbox) library is used
|
16
|
+
* for all the caching and request handling logic.
|
17
|
+
* `<platinum-sw-cache>` needs to be a child element of `<platinum-sw-register>`.
|
18
|
+
* A simple, yet useful configuration is
|
19
|
+
*
|
20
|
+
* <platinum-sw-register>
|
21
|
+
* <platinum-sw-cache></platinum-sw-cache>
|
22
|
+
* </platinum-sw-register>
|
23
|
+
*
|
24
|
+
* This is enough to have all of the (local) resources your site uses cached at runtime.
|
25
|
+
* (It uses the default `defaultCacheStrategy` of "networkFirst".)
|
26
|
+
* When there's a network available, visits to your site will go against the network copy of the
|
27
|
+
* resources, but if someone visits your site when they're offline, all the cached resources will
|
28
|
+
* be used.
|
29
|
+
*
|
30
|
+
* @demo demo/index.html An offline-capable eReader demo.
|
31
|
+
*/
|
32
|
+
Polymer({
|
33
|
+
is: 'platinum-sw-cache',
|
34
|
+
|
35
|
+
properties: {
|
36
|
+
/**
|
37
|
+
* The caching strategy used for all local (i.e. same-origin) requests.
|
38
|
+
*
|
39
|
+
* For a list of strategies, see the [`sw-toolbox` documentation](https://github.com/GoogleChrome/sw-toolbox#built-in-handlers).
|
40
|
+
* Specify a strategy as a string, without the "toolbox" prefix. E.g., for
|
41
|
+
* `toolbox.networkFirst`, set `defaultCacheStrategy` to "networkFirst".
|
42
|
+
*
|
43
|
+
* Note that the "cacheFirst" and "cacheOnly" strategies are not recommended, and may be
|
44
|
+
* explicitly prevented in a future release. More information can be found at
|
45
|
+
* https://github.com/PolymerElements/platinum-sw#cacheonly--cachefirst-defaultcachestrategy-considered-harmful
|
46
|
+
*
|
47
|
+
* @see {@link https://github.com/GoogleChrome/sw-toolbox#built-in-handlers}
|
48
|
+
*/
|
49
|
+
defaultCacheStrategy: {
|
50
|
+
type: String,
|
51
|
+
value: 'networkFirst'
|
52
|
+
},
|
53
|
+
|
54
|
+
/**
|
55
|
+
* Used to provide a list of URLs that are always precached as soon as the service worker is
|
56
|
+
* installed. Corresponds to [`sw-toolbox`'s `precache()` method](https://github.com/GoogleChrome/sw-toolbox#toolboxprecachearrayofurls).
|
57
|
+
*
|
58
|
+
* This is useful for URLs that that wouldn't necessarily be picked up by runtime caching,
|
59
|
+
* i.e. a list of resources that are needed by one of the subpages of your site, or a list of
|
60
|
+
* resources that are only loaded via user interaction.
|
61
|
+
*
|
62
|
+
* `precache` can be used in conjunction with `precacheFile`, and the two arrays will be
|
63
|
+
* concatenated.
|
64
|
+
*
|
65
|
+
* @see {@link https://github.com/GoogleChrome/sw-toolbox#toolboxprecachearrayofurls}
|
66
|
+
*/
|
67
|
+
precache: {
|
68
|
+
type: Array,
|
69
|
+
value: function() { return []; }
|
70
|
+
},
|
71
|
+
|
72
|
+
/**
|
73
|
+
* Used to provide a list of URLs that are always precached as soon as the service worker is
|
74
|
+
* installed. Corresponds to [`sw-toolbox`'s `precache()` method](https://github.com/GoogleChrome/sw-toolbox#toolboxprecachearrayofurls).
|
75
|
+
*
|
76
|
+
* While the `precache` option supports provided the array of URLs in as an inline attribute,
|
77
|
+
* this option supports providing them as an array in JSON file, which is fetched at runtime.
|
78
|
+
* This is useful if the array is generated via a separate build step, as it's easier to
|
79
|
+
* write that output to a file then it is to modify inline HTML content.
|
80
|
+
*
|
81
|
+
* `precacheFile` can be used in conjunction with `precache`, and the two arrays will be
|
82
|
+
* concatenated.
|
83
|
+
*
|
84
|
+
* @see {@link https://github.com/GoogleChrome/sw-toolbox#toolboxprecachearrayofurls}
|
85
|
+
*/
|
86
|
+
precacheFile: String
|
87
|
+
},
|
88
|
+
|
89
|
+
_getParameters: function(baseURI) {
|
90
|
+
return new Promise(function(resolve) {
|
91
|
+
var params = {
|
92
|
+
importscriptLate: new URL('bootstrap/sw-toolbox-setup.js', baseURI).href,
|
93
|
+
defaultCacheStrategy: this.defaultCacheStrategy,
|
94
|
+
precache: this.precache
|
95
|
+
};
|
96
|
+
|
97
|
+
if (this.precacheFile) {
|
98
|
+
window.fetch(this.precacheFile).then(function(response) {
|
99
|
+
if (!response.ok) {
|
100
|
+
throw Error('unable to load ' + this.precacheFile);
|
101
|
+
}
|
102
|
+
return response.json();
|
103
|
+
}.bind(this)).then(function(files) {
|
104
|
+
params.precache = params.precache.concat(files);
|
105
|
+
}).catch(function(error) {
|
106
|
+
console.info('Skipping precaching: ' + error.message);
|
107
|
+
}).then(function() {
|
108
|
+
resolve(params);
|
109
|
+
});
|
110
|
+
} else {
|
111
|
+
resolve(params);
|
112
|
+
}
|
113
|
+
}.bind(this));
|
114
|
+
}
|
115
|
+
});
|
116
|
+
</script>
|