|
19 | 19 | <div class="app"></div> |
20 | 20 | <script src="./js/app.js"></script> |
21 | 21 | <script> |
22 | | - var frame = document.createElement('iframe'); |
23 | | - frame.style.display = "none"; |
24 | | - frame.src = 'sw.html?' + Date.now(); |
25 | | - document.body.appendChild(frame); |
| 22 | + if (/https/.test(location.protocol) && 'serviceWorker' in navigator) { |
| 23 | + if (!Cache.prototype.add) { |
| 24 | + Cache.prototype.add = function add(request) { |
| 25 | + return this.addAll([request]); |
| 26 | + }; |
| 27 | + } |
| 28 | + |
| 29 | + if (!Cache.prototype.addAll) { |
| 30 | + Cache.prototype.addAll = function addAll(requests) { |
| 31 | + var cache = this; |
| 32 | + |
| 33 | + // Since DOMExceptions are not constructable: |
| 34 | + function NetworkError(message) { |
| 35 | + this.name = 'NetworkError'; |
| 36 | + this.code = 19; |
| 37 | + this.message = message; |
| 38 | + } |
| 39 | + NetworkError.prototype = Object.create(Error.prototype); |
| 40 | + |
| 41 | + return Promise.resolve().then(function() { |
| 42 | + if (arguments.length < 1) throw new TypeError(); |
| 43 | + |
| 44 | + // Simulate sequence<(Request or USVString)> binding: |
| 45 | + var sequence = []; |
| 46 | + |
| 47 | + requests = requests.map(function(request) { |
| 48 | + if (request instanceof Request) { |
| 49 | + return request; |
| 50 | + } |
| 51 | + else { |
| 52 | + return String(request); // may throw TypeError |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + return Promise.all( |
| 57 | + requests.map(function(request) { |
| 58 | + if (typeof request === 'string') { |
| 59 | + request = new Request(request); |
| 60 | + } |
| 61 | + |
| 62 | + var scheme = new URL(request.url).protocol; |
| 63 | + |
| 64 | + if (scheme !== 'http:' && scheme !== 'https:') { |
| 65 | + throw new NetworkError("Invalid scheme"); |
| 66 | + } |
| 67 | + |
| 68 | + return fetch(request.clone()); |
| 69 | + }) |
| 70 | + ); |
| 71 | + }).then(function(responses) { |
| 72 | + // TODO: check that requests don't overwrite one another |
| 73 | + // (don't think this is possible to polyfill due to opaque responses) |
| 74 | + return Promise.all( |
| 75 | + responses.map(function(response, i) { |
| 76 | + return cache.put(requests[i], response); |
| 77 | + }) |
| 78 | + ); |
| 79 | + }).then(function() { |
| 80 | + return undefined; |
| 81 | + }); |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + if (!CacheStorage.prototype.match) { |
| 86 | + // This is probably vulnerable to race conditions (removing caches etc) |
| 87 | + CacheStorage.prototype.match = function match(request, opts) { |
| 88 | + var caches = this; |
| 89 | + |
| 90 | + return this.keys().then(function(cacheNames) { |
| 91 | + var match; |
| 92 | + |
| 93 | + return cacheNames.reduce(function(chain, cacheName) { |
| 94 | + return chain.then(function() { |
| 95 | + return match || caches.open(cacheName).then(function(cache) { |
| 96 | + return cache.match(request, opts); |
| 97 | + }).then(function(response) { |
| 98 | + match = response; |
| 99 | + return match; |
| 100 | + }); |
| 101 | + }); |
| 102 | + }, Promise.resolve()); |
| 103 | + }); |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + if ('serviceWorker' in navigator) { |
| 108 | + // Delay registration until after the page has loaded, to ensure that our |
| 109 | + // precaching requests don't degrade the first visit experience. |
| 110 | + // See https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/registration |
| 111 | + window.addEventListener('load', function() { |
| 112 | + // Your service-worker.js *must* be located at the top-level directory relative to your site. |
| 113 | + // It won't be able to control pages unless it's located at the same level or higher than them. |
| 114 | + // *Don't* register service worker file in, e.g., a scripts/ sub-directory! |
| 115 | + // See https://github.com/slightlyoff/ServiceWorker/issues/468 |
| 116 | + navigator.serviceWorker.register('sw.js').then(function(reg) { |
| 117 | + setInterval(reg.update, 60 * 60 * 1000); // update the service worker hourly |
| 118 | + // updatefound is fired if service-worker.js changes. |
| 119 | + reg.onupdatefound = function() { |
| 120 | + // The updatefound event implies that reg.installing is set; see |
| 121 | + // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-container-updatefound-event |
| 122 | + var installingWorker = reg.installing; |
| 123 | + |
| 124 | + installingWorker.onstatechange = function() { |
| 125 | + switch (installingWorker.state) { |
| 126 | + case 'installed': |
| 127 | + if (navigator.serviceWorker.controller) { |
| 128 | + // At this point, the old content will have been purged and the fresh content will |
| 129 | + // have been added to the cache. |
| 130 | + // It's the perfect time to display a "New content is available; please refresh." |
| 131 | + // message in the page's interface. |
| 132 | + console.log('New or updated content is available.'); |
| 133 | + } else { |
| 134 | + // At this point, everything has been precached. |
| 135 | + // It's the perfect time to display a "Content is cached for offline use." message. |
| 136 | + console.log('Content is now available offline!'); |
| 137 | + } |
| 138 | + break; |
| 139 | + |
| 140 | + case 'redundant': |
| 141 | + console.error('The installing service worker became redundant.'); |
| 142 | + break; |
| 143 | + } |
| 144 | + }; |
| 145 | + }; |
| 146 | + }).catch(function(e) { |
| 147 | + console.error('Error during service worker registration:', e); |
| 148 | + }); |
| 149 | + }); |
| 150 | + } |
| 151 | + } |
26 | 152 | </script> |
27 | 153 | </body> |
28 | 154 | </html> |
0 commit comments