polymer-platinum-rails 1.0.0.pre.rc.1
Sign up to get free protection for your applications and to get access to all the features.
- 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,14 @@
|
|
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
|
+
|
10
|
+
<!-- Facilitates importing the full collection of platinum-sw elements. -->
|
11
|
+
<link rel="import" href="platinum-sw-cache.html">
|
12
|
+
<link rel="import" href="platinum-sw-fetch.html">
|
13
|
+
<link rel="import" href="platinum-sw-import-script.html">
|
14
|
+
<link rel="import" href="platinum-sw-register.html">
|
@@ -0,0 +1,130 @@
|
|
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-fetch>` element creates custom [`fetch` event](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#fetch-event-section)
|
14
|
+
* handlers for given URL patterns. Possible use cases include:
|
15
|
+
*
|
16
|
+
* - Using a special caching strategy for specific URLs.
|
17
|
+
* - Returning static "fallback" responses instead of network errors when a remote API
|
18
|
+
* is unavailable.
|
19
|
+
*
|
20
|
+
* In short, any scenario in which you'd like a service worker to intercept network
|
21
|
+
* requests and provide custom response handling.
|
22
|
+
*
|
23
|
+
* If you'd like a single caching policy applied to all same-origin requests, then an alternative
|
24
|
+
* to using `<platinum-sw-fetch>` is to use `<platinum-sw-cache>` with the `defaultCacheStategy`
|
25
|
+
* property set.
|
26
|
+
*
|
27
|
+
* Under the hood, the [sw-toolbox](https://github.com/googlechrome/sw-toolbox) library is used
|
28
|
+
* for all the request handling logic.
|
29
|
+
*
|
30
|
+
* `<platinum-sw-fetch>` needs to be a child element of `<platinum-sw-register>`.
|
31
|
+
*
|
32
|
+
* An example configuration is:
|
33
|
+
*
|
34
|
+
* <platinum-sw-register>
|
35
|
+
* <platinum-sw-import-script href="custom-fetch-handler.js"></platinum-sw-import-script>
|
36
|
+
* <platinum-sw-fetch handler="customFetchHandler"
|
37
|
+
* path="/(.*)/customFetch"></platinum-sw-fetch>
|
38
|
+
* </platinum-sw-register>
|
39
|
+
*
|
40
|
+
* This implies that there's a `custom-fetch-handler.js` file in the same directory as the current
|
41
|
+
* page, which defines a `sw-toolbox` compliant
|
42
|
+
* [request handler](https://github.com/googlechrome/sw-toolbox#request-handlers) named
|
43
|
+
* `customFetchHandler`. This definition is imported using `<platinum-sw-import-script>`. The
|
44
|
+
* `<platinum-sw-fetch>` element takes care of mapping which request paths are handled by
|
45
|
+
* `customFetchHandler`.
|
46
|
+
*
|
47
|
+
* Anything not matching the `path` pattern is ignored by `<platinum-sw-fetch>`,
|
48
|
+
* and it's possible to have multiple `<platinum-sw-fetch>` elements that each define different
|
49
|
+
* paths and different handlers. The path matching is performed top-down, starting with the first
|
50
|
+
* `<platinum-sw-fetch>` element.
|
51
|
+
*/
|
52
|
+
Polymer({
|
53
|
+
is: 'platinum-sw-fetch',
|
54
|
+
|
55
|
+
properties: {
|
56
|
+
/**
|
57
|
+
* The name of the request handler to use. This should be a `sw-toolbox`-style
|
58
|
+
* [request handler](https://github.com/googlechrome/sw-toolbox#request-handlers).
|
59
|
+
*
|
60
|
+
* `handler` is a `String`, not a `function`, so you're providing the name of a function, not
|
61
|
+
* the function itself. It can be a function defined in the
|
62
|
+
* [`toolbox` scope](https://github.com/googlechrome/sw-toolbox#built-in-handlers)
|
63
|
+
* (e.g. 'networkFirst', 'fastest', 'networkOnly', etc.) or a function defined in the
|
64
|
+
* [`ServiceWorkerGlobalScope`](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#service-worker-global-scope),
|
65
|
+
* like a function that is defined in a file that's imported via `platinum-sw-import-script`.
|
66
|
+
**
|
67
|
+
* @see {@link https://github.com/GoogleChrome/sw-toolbox#built-in-handlers}
|
68
|
+
*/
|
69
|
+
handler: String,
|
70
|
+
|
71
|
+
/**
|
72
|
+
* By default, `path` will only match URLs under the current host (i.e. same-origin requests).
|
73
|
+
* If you'd like to apply `handler` to cross-origin requests, then use `origin` to specify
|
74
|
+
* which hosts will match. Setting `origin` is optional.
|
75
|
+
*
|
76
|
+
* `origin` is a `String`, but it is used internally to construct a
|
77
|
+
* [`RegExp` object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions),
|
78
|
+
* which is used for the matching. To properly escape RegExp sequences like `\.`, it's
|
79
|
+
* necessary to add in an extra level of string-escaping first ('\\.').
|
80
|
+
*
|
81
|
+
* Note that the `origin` value will be matched against the full domain name and the protocol.
|
82
|
+
* If you want to match 'http' and 'https', then use 'https?://' at the start of your string.
|
83
|
+
*
|
84
|
+
* Some examples:
|
85
|
+
* - `origin="https?://.+\\.google\\.com"` → a RegExp that matches `http` or `https` requests
|
86
|
+
* made to any domain, as long as it ends in `.google.com`.
|
87
|
+
* - `origin="https://www\\.example\\.com" → a RegExp that will only match `https` requests to
|
88
|
+
* one domain, `www.example.com`.
|
89
|
+
*
|
90
|
+
* @see {@link https://github.com/googlechrome/sw-toolbox#toolboxrouterheadurlpattern-handler-options}
|
91
|
+
*/
|
92
|
+
origin: String,
|
93
|
+
|
94
|
+
/**
|
95
|
+
* URLs with paths matching `path` will have `handler` applied to them.
|
96
|
+
*
|
97
|
+
* By default, `path` will only match same-origin URLs. If you'd like it to match
|
98
|
+
* cross-origin URLs, use `path` in conjunction with `origin`.
|
99
|
+
*
|
100
|
+
* As explained in the
|
101
|
+
* [`sw-toolbox` docs](https://github.com/googlechrome/sw-toolbox#toolboxrouterheadurlpattern-handler-options),
|
102
|
+
* the URL path matching is done using the [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp)
|
103
|
+
* module, which is the same logic used in [Express-style routing](http://expressjs.com/guide/routing.html).
|
104
|
+
*
|
105
|
+
* In practice, you need to always use '/' as the first character of your `path`, and then
|
106
|
+
* can use '(.*)' as a wildcard.
|
107
|
+
*
|
108
|
+
* Some examples:
|
109
|
+
* - `path="/(.*)/customFetch"` → matches any path that ends with '/customFetch'.
|
110
|
+
* - `path="/customFetch(.*)"` → matches any path that starts with '/customFetch', optionally
|
111
|
+
* followed by other characters.
|
112
|
+
*
|
113
|
+
* @see {@link https://github.com/pillarjs/path-to-regexp}
|
114
|
+
*/
|
115
|
+
path: String
|
116
|
+
},
|
117
|
+
|
118
|
+
_getParameters: function(baseURI) {
|
119
|
+
return new Promise(function(resolve) {
|
120
|
+
var params = {
|
121
|
+
importscriptLate: new URL('bootstrap/sw-toolbox-setup.js', baseURI).href
|
122
|
+
};
|
123
|
+
if (this.path && this.handler) {
|
124
|
+
params.route = [this.path, this.handler, this.origin];
|
125
|
+
}
|
126
|
+
resolve(params);
|
127
|
+
}.bind(this));
|
128
|
+
}
|
129
|
+
});
|
130
|
+
</script>
|
@@ -0,0 +1,57 @@
|
|
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-import-script>` element is used to import a JavaScript file that is executed
|
14
|
+
* each time the service worker starts up.
|
15
|
+
*
|
16
|
+
* `<platinum-sw-import-script>` needs to be a child element of `<platinum-sw-register>`.
|
17
|
+
*
|
18
|
+
* A common use case is to define a custom request handler for a `fetch` event, but it can be used
|
19
|
+
* for any type of code that you want to be executed by the service worker.
|
20
|
+
*
|
21
|
+
* <platinum-sw-register>
|
22
|
+
* <platinum-sw-import-script href="custom-fetch-handler.js"></platinum-sw-import-script>
|
23
|
+
* <platinum-sw-fetch handler="customFetchHandler"
|
24
|
+
* path="/(.*)/customFetch"></platinum-sw-fetch>
|
25
|
+
* </platinum-sw-register>
|
26
|
+
*
|
27
|
+
* You can specify multiple `<platinum-sw-import-script>` elements, each one corresponding to a
|
28
|
+
* different JavaScript file. The JavaScript files will be loaded in the order in which the
|
29
|
+
* `<platinum-sw-import-script>` elements appear. Under the hood, this results in an
|
30
|
+
* [`importScripts()`](https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope/importScripts)
|
31
|
+
* call made from the context of the service worker.
|
32
|
+
*/
|
33
|
+
Polymer({
|
34
|
+
is: 'platinum-sw-import-script',
|
35
|
+
|
36
|
+
properties: {
|
37
|
+
/**
|
38
|
+
* The URL of the JavaScript file that you want imported.
|
39
|
+
*
|
40
|
+
* Relative URLs are assumed to be
|
41
|
+
* relative to the service worker's script location, which will almost always be the same
|
42
|
+
* location as the page which includes this element.
|
43
|
+
*/
|
44
|
+
href: String
|
45
|
+
},
|
46
|
+
|
47
|
+
_getParameters: function() {
|
48
|
+
return new Promise(function(resolve) {
|
49
|
+
var params = {};
|
50
|
+
if (this.href) {
|
51
|
+
params.importscript = this.href;
|
52
|
+
}
|
53
|
+
resolve(params);
|
54
|
+
}.bind(this));
|
55
|
+
}
|
56
|
+
});
|
57
|
+
</script>
|
@@ -0,0 +1,342 @@
|
|
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
|
+
(function() {
|
13
|
+
// Grab the URI of this file to use as a base when resolving relative paths.
|
14
|
+
// Fallback to './' as a default, though current browsers that don't support
|
15
|
+
// document.currentScript also don't support service workers.
|
16
|
+
var baseURI = document.currentScript ? document.currentScript.baseURI : './';
|
17
|
+
|
18
|
+
/**
|
19
|
+
* The `<platinum-sw-register>` element handles
|
20
|
+
* [service worker](http://www.html5rocks.com/en/tutorials/service-worker/introduction/)
|
21
|
+
* registration, reflects the overall service worker state, and coordinates the configuration
|
22
|
+
* provided by other Service Worker Elements.
|
23
|
+
* `<platinum-sw-register>` is used as a parent element for child elements in the
|
24
|
+
* `<platinum-sw-*>` group.
|
25
|
+
*
|
26
|
+
* <platinum-sw-register skip-waiting
|
27
|
+
* clients-claim
|
28
|
+
* auto-register
|
29
|
+
* state="{{state}}"
|
30
|
+
* on-service-worker-error="handleSWError"
|
31
|
+
* on-service-worker-updated="handleSWUpdated"
|
32
|
+
* on-service-worker-installed="handleSWInstalled">
|
33
|
+
* ...one or more <platinum-sw-*> children which share the service worker registration...
|
34
|
+
* </platinum-sw-register>
|
35
|
+
*
|
36
|
+
* Please see https://github.com/PolymerElements/platinum-sw#top-level-sw-importjs for a
|
37
|
+
* *crucial* prerequisite file you must create before `<platinum-sw-register>` can be used!
|
38
|
+
*
|
39
|
+
* @demo demo/index.html An offline-capable eReader demo.
|
40
|
+
*/
|
41
|
+
Polymer({
|
42
|
+
is: 'platinum-sw-register',
|
43
|
+
|
44
|
+
// Used as an "emergency" switch if we make breaking changes in the way <platinum-sw-register>
|
45
|
+
// talks to service-worker.js. Otherwise, it shouldn't need to change, and isn't meant to be
|
46
|
+
// kept in sync with the element's release number.
|
47
|
+
_version: '1.0',
|
48
|
+
|
49
|
+
/**
|
50
|
+
* Fired when the initial service worker installation completes successfully.
|
51
|
+
* The service worker will normally only be installed once, the first time a page with a
|
52
|
+
* `<platinum-sw-register>` element is visited in a given browser. If the same page is visited
|
53
|
+
* again, the existing service worker will be reused, and there won't be another
|
54
|
+
* `service-worker-installed` fired.
|
55
|
+
*
|
56
|
+
* @event service-worker-installed
|
57
|
+
* @param {String} A message indicating that the installation succeeded.
|
58
|
+
*/
|
59
|
+
|
60
|
+
/**
|
61
|
+
* Fired when the service worker update flow completes successfully.
|
62
|
+
* If you make changes to your `<platinum-sw-register>` configuration (i.e. by adding in new
|
63
|
+
* `<platinum-sw-*>` child elements, or changing their attributes), users who had the old
|
64
|
+
* service worker installed will get the update installed when they see the modified elements.
|
65
|
+
*
|
66
|
+
* @event service-worker-updated
|
67
|
+
* @param {String} A message indicating that the update succeeded.
|
68
|
+
*/
|
69
|
+
|
70
|
+
/**
|
71
|
+
* Fired when an error prevents the service worker installation from completing.
|
72
|
+
*
|
73
|
+
* @event service-worker-error
|
74
|
+
* @param {String} A message indicating what went wrong.
|
75
|
+
*/
|
76
|
+
|
77
|
+
properties: {
|
78
|
+
/**
|
79
|
+
* Whether this element should automatically register the corresponding service worker as
|
80
|
+
* soon as its added to a page.
|
81
|
+
*
|
82
|
+
* If set to `false`, then the service worker won't be automatically registered, and you
|
83
|
+
* must call this element's `register()` method if you want service worker functionality.
|
84
|
+
* This is useful if, for example, the service worker needs to be configured using
|
85
|
+
* information that isn't immediately available at the time the page loads.
|
86
|
+
*
|
87
|
+
* If set to `true`, the service worker will be automatically registered without having to
|
88
|
+
* call any methods.
|
89
|
+
*/
|
90
|
+
autoRegister: {
|
91
|
+
type: Boolean,
|
92
|
+
value: false
|
93
|
+
},
|
94
|
+
|
95
|
+
/**
|
96
|
+
* Whether the activated service worker should [take immediate control](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#clients-claim-method)
|
97
|
+
* of any pages under its scope.
|
98
|
+
*
|
99
|
+
* If this is `false`, the service worker won't have any effect until the next time the page
|
100
|
+
* is visited/reloaded.
|
101
|
+
* If this is `true`, it will take control and start handling events for the current page
|
102
|
+
* (and any pages under the same scope open in other tabs/windows) as soon it's active.
|
103
|
+
* @see {@link https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#clients-claim-method}
|
104
|
+
*/
|
105
|
+
clientsClaim: {
|
106
|
+
type: Boolean,
|
107
|
+
value: false
|
108
|
+
},
|
109
|
+
|
110
|
+
/**
|
111
|
+
* The service worker script that is [registered](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#navigator-service-worker-register).
|
112
|
+
* The script *should* be located at the top level of your site, to ensure that it is able
|
113
|
+
* to control all the pages on your site.
|
114
|
+
*
|
115
|
+
* It's *strongly* recommended that you create a top-level file named `sw-import.js`
|
116
|
+
* containing only:
|
117
|
+
*
|
118
|
+
* `importScripts('bower_components/platinum-sw/service-worker.js');`
|
119
|
+
*
|
120
|
+
* (adjust to match the path where your `platinum-sw` element directory can be found).
|
121
|
+
*
|
122
|
+
* This will ensure that your service worker script contains everything needed to play
|
123
|
+
* nicely with the Service Worker Elements group.
|
124
|
+
*
|
125
|
+
* @see {@link https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#navigator-service-worker-register}
|
126
|
+
*/
|
127
|
+
href: {
|
128
|
+
type: String,
|
129
|
+
value: 'sw-import.js'
|
130
|
+
},
|
131
|
+
|
132
|
+
/**
|
133
|
+
* Whether the page should be automatically reloaded (via `window.location.reload()`) when
|
134
|
+
* the service worker is successfully installed.
|
135
|
+
*
|
136
|
+
* While it's perfectly valid to continue using a page with a freshly installed service
|
137
|
+
* worker, it's a common pattern to want to reload it immediately following the install.
|
138
|
+
* This ensures that, for example, if you're using a `<platinum-sw-cache>` with an on the
|
139
|
+
* fly caching strategy, it will get a chance to intercept all the requests needed to render
|
140
|
+
* your page and store them in the cache.
|
141
|
+
*
|
142
|
+
* If you don't immediately reload your page, then any resources that were loaded before the
|
143
|
+
* service worker was installed (e.g. this `platinum-sw-register.html` file) won't be present
|
144
|
+
* in the cache until the next time the page is loaded.
|
145
|
+
*
|
146
|
+
* Note that this reload will only happen when a service worker is installed for the first
|
147
|
+
* time. If the service worker is subsequently updated, it won't trigger another reload.
|
148
|
+
*/
|
149
|
+
reloadOnInstall: {
|
150
|
+
type: Boolean,
|
151
|
+
value: false
|
152
|
+
},
|
153
|
+
|
154
|
+
/**
|
155
|
+
* The scope of the service worker, relative to the registered service worker script.
|
156
|
+
* All pages that fall under this scope will be controlled by the registered service worker.
|
157
|
+
*
|
158
|
+
* Normally, this would not need to be changed, unless you want the service worker to only
|
159
|
+
* apply to a subset of your site.
|
160
|
+
*
|
161
|
+
* @see {@link https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#navigator-service-worker-register}
|
162
|
+
*/
|
163
|
+
scope: {
|
164
|
+
type: String,
|
165
|
+
value: './'
|
166
|
+
},
|
167
|
+
|
168
|
+
/**
|
169
|
+
* Whether an updated service worker should [bypass the `waiting` state](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#service-worker-global-scope-skipwaiting)
|
170
|
+
* and immediately become `active`.
|
171
|
+
*
|
172
|
+
* Normally, during an update, the new service worker stays in the
|
173
|
+
* `waiting` state until the current page and any other tabs/windows that are using the old
|
174
|
+
* service worker are unloaded.
|
175
|
+
*
|
176
|
+
* If this is `false`, an updated service worker won't be activated until all instances of
|
177
|
+
* the old server worker have been unloaded.
|
178
|
+
*
|
179
|
+
* If this is `true`, an updated service worker will become `active` immediately.
|
180
|
+
* @see {@link https://slightlyoff.github.io/ServiceWorker/spec/service_worker/#service-worker-global-scope-skipwaiting}
|
181
|
+
*/
|
182
|
+
skipWaiting: {
|
183
|
+
type: Boolean,
|
184
|
+
value: false
|
185
|
+
},
|
186
|
+
|
187
|
+
/**
|
188
|
+
* The current state of the service worker registered by this element.
|
189
|
+
*
|
190
|
+
* One of:
|
191
|
+
* - 'installed'
|
192
|
+
* - 'updated'
|
193
|
+
* - 'error'
|
194
|
+
* - 'unsupported'
|
195
|
+
*/
|
196
|
+
state: {
|
197
|
+
notify: true,
|
198
|
+
readOnly: true,
|
199
|
+
type: String
|
200
|
+
}
|
201
|
+
},
|
202
|
+
|
203
|
+
/**
|
204
|
+
* Registers the service worker based on the configuration options in this element and any
|
205
|
+
* child elements.
|
206
|
+
*
|
207
|
+
* If you set the `autoRegister` property to `true`, then this method is called automatically
|
208
|
+
* at page load.
|
209
|
+
* It can be useful to set `autoRegister` to `false` and then explicitly call this method if
|
210
|
+
* there are options that are only configured after the page is loaded.
|
211
|
+
*/
|
212
|
+
register: function() {
|
213
|
+
if ('serviceWorker' in navigator) {
|
214
|
+
this._constructServiceWorkerUrl().then(function(serviceWorkerUrl) {
|
215
|
+
this._registerServiceWorker(serviceWorkerUrl);
|
216
|
+
}.bind(this));
|
217
|
+
} else {
|
218
|
+
this._setState('unsupported');
|
219
|
+
this.fire('service-worker-error', 'Service workers are not available in the current browser.');
|
220
|
+
}
|
221
|
+
},
|
222
|
+
|
223
|
+
_constructServiceWorkerUrl: function() {
|
224
|
+
var paramsPromises = [];
|
225
|
+
var children = Polymer.dom(this).childNodes;
|
226
|
+
for (var i = 0; i < children.length; i++) {
|
227
|
+
if (typeof children[i]._getParameters === 'function') {
|
228
|
+
paramsPromises.push(children[i]._getParameters(baseURI));
|
229
|
+
}
|
230
|
+
}
|
231
|
+
|
232
|
+
return Promise.all(paramsPromises).then(function(paramsResolutions) {
|
233
|
+
var params = {
|
234
|
+
baseURI: baseURI,
|
235
|
+
version: this._version
|
236
|
+
};
|
237
|
+
|
238
|
+
paramsResolutions.forEach(function(childParams) {
|
239
|
+
Object.keys(childParams).forEach(function(key) {
|
240
|
+
if (Array.isArray(params[key])) {
|
241
|
+
params[key].push(childParams[key]);
|
242
|
+
} else {
|
243
|
+
params[key] = [childParams[key]];
|
244
|
+
}
|
245
|
+
});
|
246
|
+
});
|
247
|
+
|
248
|
+
return params;
|
249
|
+
}.bind(this)).then(function(params) {
|
250
|
+
if (params.importscriptLate) {
|
251
|
+
if (params.importscript) {
|
252
|
+
params.importscript = params.importscript.concat(params.importscriptLate);
|
253
|
+
} else {
|
254
|
+
params.importscript = params.importscriptLate;
|
255
|
+
}
|
256
|
+
}
|
257
|
+
|
258
|
+
if (params.importscript) {
|
259
|
+
params.importscript = this._unique(params.importscript);
|
260
|
+
}
|
261
|
+
|
262
|
+
params.clientsClaim = this.clientsClaim;
|
263
|
+
params.skipWaiting = this.skipWaiting;
|
264
|
+
|
265
|
+
var serviceWorkerUrl = new URL(this.href, window.location);
|
266
|
+
// It's very important to ensure that the serialization is stable.
|
267
|
+
// Serializing the same settings should always produce the same URL.
|
268
|
+
// Serializing different settings should always produce a different URL.
|
269
|
+
// This ensures that the service worker upgrade flow is triggered when settings change.
|
270
|
+
serviceWorkerUrl.search = this._serializeUrlParams(params);
|
271
|
+
|
272
|
+
return serviceWorkerUrl;
|
273
|
+
}.bind(this));
|
274
|
+
},
|
275
|
+
|
276
|
+
_unique: function(arr) {
|
277
|
+
return arr.filter(function(item, index) {
|
278
|
+
return arr.indexOf(item) === index;
|
279
|
+
});
|
280
|
+
},
|
281
|
+
|
282
|
+
_serializeUrlParams: function(params) {
|
283
|
+
return Object.keys(params).sort().map(function(key) {
|
284
|
+
// encodeURIComponent(['a', 'b']) => 'a%2Cb',
|
285
|
+
// so this will still work when the values are Arrays.
|
286
|
+
// TODO: It won't work if the values in the Arrays have ',' characters in them.
|
287
|
+
return encodeURIComponent(key) + "=" + encodeURIComponent(params[key]);
|
288
|
+
}).join('&');
|
289
|
+
},
|
290
|
+
|
291
|
+
_registerServiceWorker: function(serviceWorkerUrl) {
|
292
|
+
navigator.serviceWorker.register(serviceWorkerUrl, {scope: this.scope}).then(function(registration) {
|
293
|
+
if (registration.active) {
|
294
|
+
this._setState('installed');
|
295
|
+
}
|
296
|
+
|
297
|
+
registration.onupdatefound = function() {
|
298
|
+
var installingWorker = registration.installing;
|
299
|
+
installingWorker.onstatechange = function() {
|
300
|
+
switch (installingWorker.state) {
|
301
|
+
case 'installed':
|
302
|
+
if (navigator.serviceWorker.controller) {
|
303
|
+
this._setState('updated');
|
304
|
+
this.fire('service-worker-updated',
|
305
|
+
'A new service worker was installed, replacing the old service worker.');
|
306
|
+
} else {
|
307
|
+
if (this.reloadOnInstall) {
|
308
|
+
window.location.reload();
|
309
|
+
} else {
|
310
|
+
this._setState('installed');
|
311
|
+
this.fire('service-worker-installed', 'A new service worker was installed.');
|
312
|
+
}
|
313
|
+
}
|
314
|
+
break;
|
315
|
+
|
316
|
+
case 'redundant':
|
317
|
+
this._setState('error');
|
318
|
+
this.fire('service-worker-error', 'The installing service worker became redundant.');
|
319
|
+
break;
|
320
|
+
}
|
321
|
+
}.bind(this);
|
322
|
+
}.bind(this);
|
323
|
+
}.bind(this)).catch(function(error) {
|
324
|
+
this._setState('error');
|
325
|
+
this.fire('service-worker-error', error.toString());
|
326
|
+
if (error.name === 'NetworkError') {
|
327
|
+
var location = serviceWorkerUrl.origin + serviceWorkerUrl.pathname;
|
328
|
+
console.error('A valid service worker script was not found at ' + location + '\n' +
|
329
|
+
'To learn how to fix this, please see\n' +
|
330
|
+
'https://github.com/PolymerElements/platinum-sw#top-level-sw-importjs');
|
331
|
+
}
|
332
|
+
}.bind(this));
|
333
|
+
},
|
334
|
+
|
335
|
+
attached: function() {
|
336
|
+
if (this.autoRegister) {
|
337
|
+
this.async(this.register);
|
338
|
+
}
|
339
|
+
}
|
340
|
+
});
|
341
|
+
})();
|
342
|
+
</script>
|