OLD | NEW |
(Empty) | |
| 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.g
ithub.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/CONTRI
BUTORS.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/PATEN
TS.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 resour
ces, 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 a
t runtime. |
| 25 * (It uses the default `defaultCacheStrategy` of "networkFirst".) |
| 26 * When there's a network available, visits to your site will go against the n
etwork copy of the |
| 27 * resources, but if someone visits your site when they're offline, all the ca
ched 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 recommend
ed, 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-de
faultcachestrategy-considered-harmful |
| 46 * |
| 47 * @see {@link https://github.com/GoogleChrome/sw-toolbox#built-in-handler
s} |
| 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#toolboxprecachea
rrayofurls} |
| 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 a
n 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#toolboxprecachea
rrayofurls} |
| 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).hr
ef, |
| 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> |
OLD | NEW |