Skip to content

Commit 987e0d7

Browse files
authored
Merge pull request #16 from FANMixco/service-worker-changes
Updated service worker
2 parents 59b8397 + fd0503d commit 987e0d7

File tree

3 files changed

+81
-156
lines changed

3 files changed

+81
-156
lines changed

index.html

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -490,35 +490,26 @@ <h4 class="hCustom" data-tag="timeMaximum"></h4>
490490
<script src="js/functions/genericActions.min.js"></script>
491491
<script src="js/lib/ua-parser.min.js"></script>
492492
<script src="js/timer.min.js"></script>
493+
493494
<script>
495+
//Translator
494496
load();
495497

496-
// This is the "Offline page" service worker
497-
498-
// Add this below content to your HTML page, or add the js file to your page at the very top to register service worker
499-
500-
// Check compatibility for the browser we're running this in
501-
if ("serviceWorker" in navigator) {
502-
if (navigator.serviceWorker.controller) {
503-
console.log("[PWA Builder] active service worker found, no need to register");
504-
} else {
505-
// Register the service worker
506-
navigator.serviceWorker
507-
.register("pwabuilder-sw.js", {
508-
scope: "./"
509-
})
510-
.then(function (reg) {
511-
console.log("[PWA Builder] Service worker has been registered for scope: " + reg.scope);
512-
});
513-
}
514-
}
515-
</script>
516-
<script>
517-
window.dataLayer = window.dataLayer || [];
498+
//Analytics
499+
window.dataLayer = window.dataLayer || [];
518500
function gtag(){dataLayer.push(arguments);}
519501
gtag('js', new Date());
520502

521503
gtag('config', 'UA-139701351-1');
522-
</script>
504+
</script>
505+
506+
<script type="module">
507+
//PWA
508+
import 'https://cdn.jsdelivr.net/npm/@pwabuilder/pwaupdate';
509+
if ("serviceWorker" in navigator) {
510+
const el = document.createElement('pwa-update');
511+
document.body.appendChild(el);
512+
}
513+
</script>
523514
</body>
524515
</html>

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@
440440
"sizes": "16x16"
441441
}
442442
],
443-
"categories": ["timer", "tools", "alarms"],
443+
"categories": ["timer", "tools", "alarms", "productivity", "utilities"],
444444
"screenshots" : [
445445
{
446446
"src": "img/previews/preview1.webp",

pwabuilder-sw.js

Lines changed: 66 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,75 @@
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
132

14-
'use strict';
3+
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
154

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";
2310

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();
3114
}
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-
);
8815
});
8916

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+
);
9728

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+
);
11340

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+
);
13652

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

Comments
 (0)