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

Commit f450a54

Browse files
committed
new rc5 version: put up scripts for service workers options #4 and #5
1 parent 0e657d9 commit f450a54

File tree

5 files changed

+141
-1
lines changed

5 files changed

+141
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//This is the service worker with the Cache-first network
2+
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
4+
if (navigator.serviceWorker.controller) {
5+
console.log('[Manifoldjs] active service worker found, no need to register')
6+
} else {
7+
8+
//Register the ServiceWorker
9+
navigator.serviceWorker.register('manifoldjs-sw.js', {
10+
scope: './'
11+
}).then(function(reg) {
12+
console.log('Service worker has been registered for scope:'+ reg.scope);
13+
});
14+
}
15+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//This is the service worker with the Cache-first network
2+
3+
//Install stage sets up the cache-array to configure pre-cache content
4+
self.addEventListener('install', function(event) {
5+
event.waitUntil(preLoad());
6+
});
7+
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']);
13+
});
14+
}
15+
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+
});
23+
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)
33+
});
34+
};
35+
36+
var addToCache = function(request){
37+
return caches.open('manifoldjs-offline').then(function (cache) {
38+
return fetch(request).then(function (response) {
39+
console.log('[manifoldjs] add page to offline'+response.url)
40+
return cache.put(request, response);
41+
});
42+
});
43+
};
44+
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+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//This is the service worker with the Cache-first network
2+
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
4+
if (navigator.serviceWorker.controller) {
5+
console.log('[Manifoldjs] active service worker found, no need to register')
6+
} else {
7+
8+
//Register the ServiceWorker
9+
navigator.serviceWorker.register('manifoldjs-sw.js', {
10+
scope: './'
11+
}).then(function(reg) {
12+
console.log('Service worker has been registered for scope:'+ reg.scope);
13+
});
14+
}
15+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//This is the service worker with the Cache-first network
2+
3+
//Install stage sets up the cache-array to configure pre-cache content
4+
self.addEventListener('install', function(event) {
5+
event.waitUntil(preLoad());
6+
});
7+
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']);
13+
});
14+
}
15+
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+
});
23+
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)
33+
});
34+
};
35+
36+
var addToCache = function(request){
37+
return caches.open('manifoldjs-offline').then(function (cache) {
38+
return fetch(request).then(function (response) {
39+
console.log('[manifoldjs] add page to offline'+response.url)
40+
return cache.put(request, response);
41+
});
42+
});
43+
};
44+
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+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pwabuilder-lib",
3-
"version": "2.0.0-rc.4",
3+
"version": "2.0.0-rc.5",
44
"description": "PWA Builder Core Library",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)