|
1 |
| -/* |
2 |
| - Copyright 2015 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 |
| -*/ |
| 1 | +//This is the service worker with the Advanced caching |
13 | 2 |
|
14 |
| -'use strict'; |
| 3 | +importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js'); |
15 | 4 |
|
16 |
| -// Incrementing CACHE_VERSION will kick off the install event and force previously cached |
17 |
| -// resources to be cached again. |
18 |
| -const CACHE_VERSION = 1; |
19 |
| -let CURRENT_CACHES = { |
20 |
| - offline: 'offline-v' + CACHE_VERSION |
21 |
| -}; |
22 |
| -const OFFLINE_URL = 'offline.html'; |
| 5 | +const HTML_CACHE = "html"; |
| 6 | +const JS_CACHE = "javascript"; |
| 7 | +const STYLE_CACHE = "stylesheets"; |
| 8 | +const IMAGE_CACHE = "images"; |
| 9 | +const FONT_CACHE = "fonts"; |
23 | 10 |
|
24 |
| -function createCacheBustedRequest(url) { |
25 |
| - let request = new Request(url, {cache: 'reload'}); |
26 |
| - // See https://fetch.spec.whatwg.org/#concept-request-mode |
27 |
| - // This is not yet supported in Chrome as of M48, so we need to explicitly check to see |
28 |
| - // if the cache: 'reload' option had any effect. |
29 |
| - if ('cache' in request) { |
30 |
| - return request; |
| 11 | +self.addEventListener("message", (event) => { |
| 12 | + if (event.data && event.data.type === "SKIP_WAITING") { |
| 13 | + self.skipWaiting(); |
31 | 14 | }
|
32 |
| - |
33 |
| - // If {cache: 'reload'} didn't have any effect, append a cache-busting URL parameter instead. |
34 |
| - let bustedUrl = new URL(url, self.location.href); |
35 |
| - bustedUrl.search += (bustedUrl.search ? '&' : '') + 'cachebust=' + Date.now(); |
36 |
| - return new Request(bustedUrl); |
37 |
| -} |
38 |
| - |
39 |
| -self.addEventListener('install', event => { |
40 |
| - event.waitUntil( |
41 |
| - // We can't use cache.add() here, since we want OFFLINE_URL to be the cache key, but |
42 |
| - // the actual URL we end up requesting might include a cache-busting parameter. |
43 |
| - fetch(createCacheBustedRequest(OFFLINE_URL)).then(function(response) { |
44 |
| - return caches.open(CURRENT_CACHES.offline).then(function(cache) { |
45 |
| - var tmpLng; |
46 |
| - try { |
47 |
| - if (!lang) |
48 |
| - tmpLng = "en"; |
49 |
| - } |
50 |
| - catch(e){ |
51 |
| - tmpLng = "en"; |
52 |
| - } |
53 |
| - return cache.addAll([ |
54 |
| - "index.html", |
55 |
| - "css/loading.min.css", |
56 |
| - "css/materialdesignicons.min.css", |
57 |
| - "css/timer.css", |
58 |
| - "css/material.indigo-pink.min.css", |
59 |
| - "css/getmdl-select.min.css", |
60 |
| - "browserSpecific/css/dialog-polyfill.css", |
61 |
| - "js/lib/material.min.js", |
62 |
| - "js/lib/translate.min.js", |
63 |
| - "js/lib/darkModeHandler.min.js", |
64 |
| - "js/lib/devicedetector-min.js", |
65 |
| - "js/components/getmdl-select.min.js", |
66 |
| - "browserSpecific/js/dialog-polyfill.js", |
67 |
| - "browserSpecific/js/jspdf.min.js", |
68 |
| - "browserSpecific/js/getOS.min.js", |
69 |
| - "browserSpecific/js/actions.min.js", |
70 |
| - "js/components/setTimeDialog.min.js", |
71 |
| - "js/lib/moment.min.js", |
72 |
| - "js/lib/ua-parser.min.js", |
73 |
| - "js/db/customDBActions.min.js", |
74 |
| - "js/db/dbHandler.min.js", |
75 |
| - "js/functions/genericActions.min.js", |
76 |
| - "js/timer.min.js", |
77 |
| - `js/lang/${tmpLng}.json`, |
78 |
| - "img/supernova.png", |
79 |
| - "img/amazon.png", |
80 |
| - "img/playStore.png", |
81 |
| - "img/microsoft.png", |
82 |
| - "img/clapping-hands.svg", |
83 |
| - "img/clapping-off.svg" |
84 |
| - ]); |
85 |
| - }); |
86 |
| - }) |
87 |
| - ); |
88 | 15 | });
|
89 | 16 |
|
90 |
| -self.addEventListener('activate', event => { |
91 |
| - // Delete all caches that aren't named in CURRENT_CACHES. |
92 |
| - // While there is only one cache in this example, the same logic will handle the case where |
93 |
| - // there are multiple versioned caches. |
94 |
| - let expectedCacheNames = Object.keys(CURRENT_CACHES).map(function(key) { |
95 |
| - return CURRENT_CACHES[key]; |
96 |
| - }); |
| 17 | +workbox.routing.registerRoute( |
| 18 | + ({event}) => event.request.destination === 'document', |
| 19 | + new workbox.strategies.NetworkFirst({ |
| 20 | + cacheName: HTML_CACHE, |
| 21 | + plugins: [ |
| 22 | + new workbox.expiration.ExpirationPlugin({ |
| 23 | + maxEntries: 10, |
| 24 | + }), |
| 25 | + ], |
| 26 | + }) |
| 27 | +); |
97 | 28 |
|
98 |
| - event.waitUntil( |
99 |
| - caches.keys().then(cacheNames => { |
100 |
| - return Promise.all( |
101 |
| - cacheNames.map(cacheName => { |
102 |
| - if (expectedCacheNames.indexOf(cacheName) === -1) { |
103 |
| - // If this cache name isn't present in the array of "expected" cache names, |
104 |
| - // then delete it. |
105 |
| - console.log('Deleting out of date cache:', cacheName); |
106 |
| - return caches.delete(cacheName); |
107 |
| - } |
108 |
| - }) |
109 |
| - ); |
110 |
| - }) |
111 |
| - ); |
112 |
| -}); |
| 29 | +workbox.routing.registerRoute( |
| 30 | + ({event}) => event.request.destination === 'script', |
| 31 | + new workbox.strategies.StaleWhileRevalidate({ |
| 32 | + cacheName: JS_CACHE, |
| 33 | + plugins: [ |
| 34 | + new workbox.expiration.ExpirationPlugin({ |
| 35 | + maxEntries: 15, |
| 36 | + }), |
| 37 | + ], |
| 38 | + }) |
| 39 | +); |
113 | 40 |
|
114 |
| -self.addEventListener('fetch', event => { |
115 |
| - // We only want to call event.respondWith() if this is a navigation request |
116 |
| - // for an HTML page. |
117 |
| - // request.mode of 'navigate' is unfortunately not supported in Chrome |
118 |
| - // versions older than 49, so we need to include a less precise fallback, |
119 |
| - // which checks for a GET request with an Accept: text/html header. |
120 |
| - if (event.request.mode === 'navigate' || |
121 |
| - (event.request.method === 'GET' && |
122 |
| - event.request.headers.get('accept').includes('text/html'))) { |
123 |
| - console.log('Handling fetch event for', event.request.url); |
124 |
| - event.respondWith( |
125 |
| - fetch(event.request).catch(error => { |
126 |
| - // The catch is only triggered if fetch() throws an exception, which will most likely |
127 |
| - // happen due to the server being unreachable. |
128 |
| - // If fetch() returns a valid HTTP response with an response code in the 4xx or 5xx |
129 |
| - // range, the catch() will NOT be called. If you need custom handling for 4xx or 5xx |
130 |
| - // errors, see https://github.com/GoogleChrome/samples/tree/gh-pages/service-worker/fallback-response |
131 |
| - console.log('Fetch failed; returning offline page instead.', error); |
132 |
| - return caches.match(OFFLINE_URL); |
133 |
| - }) |
134 |
| - ); |
135 |
| - } |
| 41 | +workbox.routing.registerRoute( |
| 42 | + ({event}) => event.request.destination === 'style', |
| 43 | + new workbox.strategies.StaleWhileRevalidate({ |
| 44 | + cacheName: STYLE_CACHE, |
| 45 | + plugins: [ |
| 46 | + new workbox.expiration.ExpirationPlugin({ |
| 47 | + maxEntries: 15, |
| 48 | + }), |
| 49 | + ], |
| 50 | + }) |
| 51 | +); |
136 | 52 |
|
137 |
| - // If our if() condition is false, then this fetch handler won't intercept the request. |
138 |
| - // If there are any other fetch handlers registered, they will get a chance to call |
139 |
| - // event.respondWith(). If no fetch handlers call event.respondWith(), the request will be |
140 |
| - // handled by the browser as if there were no service worker involvement. |
141 |
| -}); |
| 53 | +workbox.routing.registerRoute( |
| 54 | + ({event}) => event.request.destination === 'image', |
| 55 | + new workbox.strategies.StaleWhileRevalidate({ |
| 56 | + cacheName: IMAGE_CACHE, |
| 57 | + plugins: [ |
| 58 | + new workbox.expiration.ExpirationPlugin({ |
| 59 | + maxEntries: 15, |
| 60 | + }), |
| 61 | + ], |
| 62 | + }) |
| 63 | +); |
| 64 | + |
| 65 | +workbox.routing.registerRoute( |
| 66 | + ({event}) => event.request.destination === 'font', |
| 67 | + new workbox.strategies.StaleWhileRevalidate({ |
| 68 | + cacheName: FONT_CACHE, |
| 69 | + plugins: [ |
| 70 | + new workbox.expiration.ExpirationPlugin({ |
| 71 | + maxEntries: 15, |
| 72 | + }), |
| 73 | + ], |
| 74 | + }) |
| 75 | +); |
0 commit comments