Skip to content
This repository was archived by the owner on Jan 14, 2022. It is now read-only.

Commit 5088c43

Browse files
committed
Updated Service Worker #4 with latest code
1 parent f450a54 commit 5088c43

File tree

2 files changed

+52
-44
lines changed

2 files changed

+52
-44
lines changed
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
//This is the service worker with the Cache-first network
22

3-
//Add this below content to your HTML page, or add the js file to your page at the very top to register service worker
3+
//Add this below content to your HTML page, or add the js file to your page at the very top to register sercie worker
44
if (navigator.serviceWorker.controller) {
5-
console.log('[Manifoldjs] active service worker found, no need to register')
5+
console.log('[PWA Builder] active service worker found, no need to register')
66
} else {
77

88
//Register the ServiceWorker
9-
navigator.serviceWorker.register('manifoldjs-sw.js', {
9+
navigator.serviceWorker.register('pwabuilder-sw.js', {
1010
scope: './'
1111
}).then(function(reg) {
1212
console.log('Service worker has been registered for scope:'+ reg.scope);
1313
});
14-
}
15-
14+
}
Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,64 @@
11
//This is the service worker with the Cache-first network
22

3+
var CACHE = 'pwabuilder-precache';
4+
var precacheFiles = [
5+
/* Add an array of files to precache for your app */
6+
];
7+
38
//Install stage sets up the cache-array to configure pre-cache content
4-
self.addEventListener('install', function(event) {
5-
event.waitUntil(preLoad());
9+
self.addEventListener('install', function(evt) {
10+
console.log('The service worker is being installed.');
11+
evt.waitUntil(precache().then(function() {
12+
console.log('[ServiceWorker] Skip waiting on install');
13+
return self.skipWaiting();
14+
15+
})
16+
);
17+
});
18+
19+
20+
//allow sw to control of current page
21+
self.addEventListener('activate', function(event) {
22+
console.log('[ServiceWorker] Claiming clients for current page');
23+
return self.clients.claim();
24+
25+
});
26+
27+
self.addEventListener('fetch', function(evt) {
28+
console.log('The service worker is serving the asset.'+ evt.request.url);
29+
evt.respondWith(fromCache(evt.request).catch(fromServer(evt.request)));
30+
evt.waitUntil(update(evt.request));
631
});
732

8-
var preLoad = function(){
9-
console.log('[Manifoldjs] Install Event processing');
10-
return caches.open('manifoldjs-offline').then(function(cache) {
11-
console.log('[Manifoldjs] Cached index and offline page during Install');
12-
return cache.addAll(['/offline.html', '/index.html']);
33+
34+
function precache() {
35+
return caches.open(CACHE).then(function (cache) {
36+
return cache.addAll(precacheFiles);
1337
});
1438
}
1539

16-
self.addEventListener('fetch', function(event) {
17-
console.log('The service worker is serving the asset.');
18-
event.respondWith(checkResponse(event.request).catch(function() {
19-
return returnFromCache(event.request)}
20-
));
21-
event.waitUntil(addToCache(event.request));
22-
});
2340

24-
var checkResponse = function(request){
25-
return new Promise(function(fulfill, reject) {
26-
fetch(request).then(function(response){
27-
if(response.status !== 404) {
28-
fulfill(response)
29-
} else {
30-
reject()
31-
}
32-
}, reject)
41+
function fromCache(request) {
42+
//we pull files from the cache first thing so we can show them fast
43+
return caches.open(CACHE).then(function (cache) {
44+
return cache.match(request).then(function (matching) {
45+
return matching || Promise.reject('no-match');
46+
});
3347
});
34-
};
48+
}
3549

36-
var addToCache = function(request){
37-
return caches.open('manifoldjs-offline').then(function (cache) {
50+
51+
function update(request) {
52+
//this is where we call the server to get the newest version of the
53+
//file to use the next time we show view
54+
return caches.open(CACHE).then(function (cache) {
3855
return fetch(request).then(function (response) {
39-
console.log('[manifoldjs] add page to offline'+response.url)
4056
return cache.put(request, response);
4157
});
4258
});
43-
};
59+
}
4460

45-
var returnFromCache = function(request){
46-
return caches.open('manifoldjs-offline').then(function (cache) {
47-
return cache.match(request).then(function (matching) {
48-
if(!matching || matching.status == 404) {
49-
return cache.match('offline.html')
50-
} else {
51-
return matching
52-
}
53-
});
54-
});
55-
};
61+
function fromServer(request){
62+
//this is the fallback if it is not in the cahche to go to the server and get it
63+
return fetch(request).then(function(response){ return response})
64+
}

0 commit comments

Comments
 (0)