-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
72 lines (66 loc) · 2.38 KB
/
sw.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
// sw.js Cache Name to keep in browser and session settings
const CACHE_NAME = 'offline-first-v1';
let sessionId = null;
self.addEventListener('message', (event) => {
if (event.data && event.data.sessionId) {
sessionId = event.data.sessionId;
}
});
// This is install listner to keep pages in alive in cache
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then(function(cache) {
if (sessionId) {
return cache.addAll([
'/',
'https://apex.oracle.com/pls/apex/r/pwa_app_sample/testing/home?session=' + sessionId,
'https://apex.oracle.com/pls/apex/r/pwa_app_sample/testing/testing-search?session=' + sessionId,
'https://apex.oracle.com/pls/apex/r/pwa_app_sample/testing/testing-report?session=' + sessionId
]);
} else {
return Promise.resolve();
}
})
);
});
// This is activate listner
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.filter(function(cacheName) {
return cacheName.startsWith('offline-first-') &&
cacheName !== CACHE_NAME;
}).map(function(cacheName) {
return caches.delete(cacheName);
})
);
})
);
});
// This is fetch listner
self.addEventListener('fetch', (event) => {
if (!navigator.onLine) {
event.respondWith(fetchFromCache(event.request));
} else {
event.respondWith(fetchFromServer(event.request));
}
});
// This listner is to request cache
async function fetchFromCache(request) {
const cache = await caches.open(CACHE_NAME);
const cachedResponse = await cache.match(request);
return cachedResponse || fetch(request);
}
// This listner is to request server
async function fetchFromServer(request) {
const cache = await caches.open(CACHE_NAME);
try {
const response = await fetch(request);
cache.put(request, response.clone());
return response;
} catch (error) {
const cachedResponse = await cache.match(request);
return cachedResponse || new Response('Offline');
}
}