1/*
2 Copyright 2016 Google Inc. All Rights Reserved.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software
8 distributed under the License is distributed on an "AS IS" BASIS,
9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 See the License for the specific language governing permissions and
11 limitations under the License.
12 */
13
14// Names of the two caches used in this version of the service worker.
15// Change to v2, etc. when you update any of the local resources, which will
16// in turn trigger the install event again.
17const PRECACHE = 'precache-v1';
18const RUNTIME = 'runtime';
19
20// A list of local resources we always want to be cached.
21const PRECACHE_URLS = [
22 'index.html',
23 './', // Alias for index.html
24 'styles.css',
25 '../../styles/main.css',
26 'demo.js'
27];
28
29// The install handler takes care of precaching the resources we always need.
30self.addEventListener('install', event => {
31 event.waitUntil(
32 caches.open(PRECACHE)
33 .then(cache => cache.addAll(PRECACHE_URLS))
34 .then(self.skipWaiting())
35 );
36});
37
38// The activate handler takes care of cleaning up old caches.
39self.addEventListener('activate', event => {
40 const currentCaches = [PRECACHE, RUNTIME];
41 event.waitUntil(
42 caches.keys().then(cacheNames => {
43 return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
44 }).then(cachesToDelete => {
45 return Promise.all(cachesToDelete.map(cacheToDelete => {
46 return caches.delete(cacheToDelete);
47 }));
48 }).then(() => self.clients.claim())
49 );
50});
51
52// The fetch handler serves responses for same-origin resources from a cache.
53// If no response is found, it populates the runtime cache with the response
54// from the network before returning it to the page.
55self.addEventListener('fetch', event => {
56 // Skip cross-origin requests, like those for Google Analytics.
57 if (event.request.url.startsWith(self.location.origin)) {
58 event.respondWith(
59 caches.match(event.request).then(cachedResponse => {
60 if (cachedResponse) {
61 return cachedResponse;
62 }
63
64 return caches.open(RUNTIME).then(cache => {
65 return fetch(event.request).then(response => {
66 // Put a copy of the response in the runtime cache.
67 return cache.put(event.request, response.clone()).then(() => {
68 return response;
69 });
70 });
71 });
72 })
73 );
74 }
75});
1if ('serviceWorker' in navigator) { window.addEventListener('load', function() { navigator.serviceWorker.register('/sw.js').then(function(registration) { // Registration was successful console.log('ServiceWorker registration successful with scope: ', registration.scope); }, function(err) { // registration failed :( console.log('ServiceWorker registration failed: ', err); }); });}