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

Commit 168a4c6

Browse files
authored
Merge pull request #36 from manifoldjs/v2.0.0-development
Merge V2.0.0 development into master branch
2 parents 1b56db1 + f7e0869 commit 168a4c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2749
-441
lines changed

LICENSE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
manifoldjs-lib
1+
pwabuilder-lib
22

33
Copyright (c) Microsoft Corporation
44

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
# manifoldjs-lib
1+
# pwabuilder-lib
22

3-
## ManifoldJS Core Library
3+
## PWA Builder Core Library
44

5-
The library contains the core modules required by [ManifoldJS](https://github.com/manifoldjs/ManifoldJS), a tool for creating hosted web applications based on a [W3C Web App manifest](http://www.w3.org/TR/appmanifest/).
5+
The library contains the core modules required by [PWA Builder](https://github.com/manifoldjs/ManifoldJS), a tool for creating hosted web applications based on a [W3C Web App manifest](http://www.w3.org/TR/appmanifest/).
66

77
## Installation
88

99
```
10-
npm install manifoldjs-lib
10+
npm install pwabuilder-lib
1111
```
1212
In node.js:
1313

1414
```
15-
var lib = require('manifoldjs-lib')
15+
var lib = require('pwabuilder-lib')
1616
```
1717

1818
## Documentation
1919
To get started, visit our [wiki](https://github.com/manifoldjs/ManifoldJS/wiki).
2020

2121
## License
2222

23-
> manifoldjs-lib
23+
> pwabuilder-lib
2424
2525
> Copyright (c) Microsoft Corporation
2626
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//This is the "Offline page" service worker
2+
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
4+
if (navigator.serviceWorker.controller) {
5+
console.log('[PWA Builder] active service worker found, no need to register')
6+
} else {
7+
//Register the ServiceWorker
8+
navigator.serviceWorker.register('pwabuider-sw.js', {
9+
scope: './'
10+
}).then(function(reg) {
11+
console.log('Service worker has been registered for scope:'+ reg.scope);
12+
});
13+
}
14+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//This is the "Offline page" service worker
2+
3+
//Install stage sets up the offline page in the cahche and opens a new cache
4+
self.addEventListener('install', function(event) {
5+
var offlinePage = new Request('offline.html');
6+
event.waitUntil(
7+
fetch(offlinePage).then(function(response) {
8+
return caches.open('pwabuilder-offline').then(function(cache) {
9+
console.log('[PWA Builder] Cached offline page during Install'+ response.url);
10+
return cache.put(offlinePage, response);
11+
});
12+
}));
13+
});
14+
15+
//If any fetch fails, it will show the offline page.
16+
//Maybe this should be limited to HTML documents?
17+
self.addEventListener('fetch', function(event) {
18+
event.respondWith(
19+
fetch(event.request).catch(function(error) {
20+
console.error( '[PWA Builder] Network request Failed. Serving offline page ' + error );
21+
return caches.open('pwabuilder-offline').then(function(cache) {
22+
return cache.match('offline.html');
23+
});
24+
}));
25+
});
26+
27+
//This is a event that can be fired from your page to tell the SW to update the offline page
28+
self.addEventListener('refreshOffline', function(response) {
29+
return caches.open('pwabuilder-offline').then(function(cache) {
30+
console.log('[PWA Builder] Offline page updated from refreshOffline event: '+ response.url);
31+
return cache.put(offlinePage, response);
32+
});
33+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//This is the "Offline copy of pages" service worker
2+
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
4+
if (navigator.serviceWorker.controller) {
5+
console.log('[PWA Builder] active service worker found, no need to register')
6+
} else {
7+
//Register the ServiceWorker
8+
navigator.serviceWorker.register('pwabuilder-sw.js', {
9+
scope: './'
10+
}).then(function(reg) {
11+
console.log('Service worker has been registered for scope:'+ reg.scope);
12+
});
13+
}
14+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//This is the "Offline copy of pages" wervice worker
2+
3+
//Install stage sets up the index page (home page) in the cahche and opens a new cache
4+
self.addEventListener('install', function(event) {
5+
var indexPage = new Request('index.html');
6+
event.waitUntil(
7+
fetch(indexPage).then(function(response) {
8+
return caches.open('pwabuilder-offline').then(function(cache) {
9+
console.log('[PWA Builder] Cached index page during Install'+ response.url);
10+
return cache.put(indexPage, response);
11+
});
12+
}));
13+
});
14+
15+
//If any fetch fails, it will look for the request in the cache and serve it from there first
16+
self.addEventListener('fetch', function(event) {
17+
var updateCache = function(request){
18+
return caches.open('pwabuilder-offline').then(function (cache) {
19+
return fetch(request).then(function (response) {
20+
console.log('[PWA Builder] add page to offline'+response.url)
21+
return cache.put(request, response);
22+
});
23+
});
24+
};
25+
26+
event.waitUntil(updateCache(event.request));
27+
28+
event.respondWith(
29+
fetch(event.request).catch(function(error) {
30+
console.log( '[PWA Builder] Network request Failed. Serving content from cache: ' + error );
31+
32+
//Check to see if you have it in the cache
33+
//Return response
34+
//If not in the cache, then return error page
35+
return caches.open('pwabuilder-offline').then(function (cache) {
36+
return cache.match(event.request).then(function (matching) {
37+
var report = !matching || matching.status == 404?Promise.reject('no-match'): matching;
38+
return report
39+
});
40+
});
41+
})
42+
);
43+
})
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 combined offline experience (Offline page + Offline copy of pages)
2+
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
4+
if (navigator.serviceWorker.controller) {
5+
console.log('[PWA Builder] active service worker found, no need to register')
6+
} else {
7+
8+
//Register the ServiceWorker
9+
navigator.serviceWorker.register('pwabuilder-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 combined offline experience (Offline page + Offline copy of pages)
2+
3+
//Install stage sets up the offline page in the cahche and opens a new cache
4+
self.addEventListener('install', function(event) {
5+
event.waitUntil(preLoad());
6+
});
7+
8+
var preLoad = function(){
9+
console.log('[PWA Builder] Install Event processing');
10+
return caches.open('pwabuilder-offline').then(function(cache) {
11+
console.log('[PWA Builder] 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('pwabuilder-offline').then(function (cache) {
38+
return fetch(request).then(function (response) {
39+
console.log('[PWA Builder] 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('pwabuilder-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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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 sercie worker
4+
if (navigator.serviceWorker.controller) {
5+
console.log('[PWA Builder] active service worker found, no need to register')
6+
} else {
7+
8+
//Register the ServiceWorker
9+
navigator.serviceWorker.register('pwabuilder-sw.js', {
10+
scope: './'
11+
}).then(function(reg) {
12+
console.log('Service worker has been registered for scope:'+ reg.scope);
13+
});
14+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//This is the service worker with the Cache-first network
2+
3+
var CACHE = 'pwabuilder-precache';
4+
var precacheFiles = [
5+
/* Add an array of files to precache for your app */
6+
];
7+
8+
//Install stage sets up the cache-array to configure pre-cache content
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));
31+
});
32+
33+
34+
function precache() {
35+
return caches.open(CACHE).then(function (cache) {
36+
return cache.addAll(precacheFiles);
37+
});
38+
}
39+
40+
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+
});
47+
});
48+
}
49+
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) {
55+
return fetch(request).then(function (response) {
56+
return cache.put(request, response);
57+
});
58+
});
59+
}
60+
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)