-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
100 lines (97 loc) · 3.93 KB
/
service-worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// var dataCacheName = 'pwa-olx-webapp';
// var cacheName = 'pwa-olx-webapp-final-1';
// var filesToCache = [
// '/',
// '/index.html',
// '/ads-details.html',
// '/ads.html',
// '/chat.html',
// '/notifications.html',
// '/Register.html',
// '/SignIn.html',
// '/stylesheets/bootstrap.css',
// '/stylesheets/chat.css',
// '/stylesheets/fontawesome.css',
// '/stylesheets/forms.css',
// '/stylesheets/navbar.css',
// '/stylesheets/posts.css',
// '/javascript/adsdata.js',
// '/javascript/app.js',
// '/javascript/getAds.js',
// '/javascript/logsession.js',
// '/javascript/logsession1.js',
// '/javascript/chat.js',
// '/javascript/notify.js'
// ];
// after a service worker is installed and the user navigates to a different page or
// refreshes,the service worker will begin to receive fetch events
self.addEventListener('fetch', function (event) {
event.respondWith(caches.open('cache').then(function (cache) {
return cache.match(event.request).then(function (response) {
console.log("cache request: " + event.request.url);
var fetchPromise = fetch(event.request).then(function (networkResponse) {
// if we got a response from the cache, update the cache
console.log("fetch completed: " + event.request.url, networkResponse);
if (networkResponse) {
console.debug("updated cached page: " + event.request.url, networkResponse);
cache.put(event.request, networkResponse.clone());
}
return networkResponse;
}, function (event) {
// rejected promise - just ignore it, we're offline!
console.log("Error in fetch()", event);
event.waitUntil(
caches.open('cache').then(function (cache) {
// our cache is named *cache* in the caches.open() above
return cache.addAll
([
//cache.addAll(), takes a list of URLs, then fetches them from the server
// and adds the response to the cache.
// add your entire site to the cache- as in the code below; for offline access
// If you have some build process for your site, perhaps that could
// generate the list of possible URLs that a user might load.
'/',
'/index.html',
'/ads-details.html',
'/ads.html',
'/chat.html',
'/notifications.html',
'/Register.html',
'/SignIn.html',
'/my-favorite.html',
'/stylesheets/bootstrap.css',
'/stylesheets/chat.css',
'/stylesheets/fontawesome.css',
'/stylesheets/forms.css',
'/stylesheets/navbar.css',
'/stylesheets/posts.css',
'/javascript/adsdata.js',
'/javascript/app.js',
'/javascript/getAds.js',
'/javascript/logsession.js',
'/javascript/logsession1.js',
'/javascript/chat.js',
'/javascript/notify.js',
'/javascript/favorite.js',
// Do not replace/delete/edit the service-worker.js/ and manifest.js paths below
'/service-worker.js',
'/manifest.js',
//These are links to the extenal social media buttons that should be cached;
// we have used twitter's as an example
]);
})
);
});
// respond from the cache, or the network
return response || fetchPromise;
});
}));
});
// window.addEventListener('beforeinstallprompt', (e) => {
// // Prevent Chrome 67 and earlier from automatically showing the prompt
// e.preventDefault();
// // Stash the event so it can be triggered later.
// deferredPrompt = e;
// // Update UI notify the user they can add to home screen
// btnAdd.style.display = 'block';
// });