-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-workers.js
129 lines (116 loc) · 3.89 KB
/
service-workers.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* code service worker only cache first page offline
*/
importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.2.0/workbox-sw.js');
const CACHE_NAME = 'Freelanceku';
workbox.routing.registerRoute(
/\.(?:js|css|png|jpg|jpeg|svg|gif)$/,
new workbox.strategies.CacheFirst({
cacheName: CACHE_NAME,
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
],
})
);
const OFFLINE_URL = 'Public/dist/pwa/offline.php';
const OFFLINE_IMAGE_URL = 'Public/dist/pwa/offline.png';
self.addEventListener('install', event => {
const urlsToCache = [
OFFLINE_URL,
OFFLINE_IMAGE_URL
];
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
return cache.addAll(urlsToCache);
})
.then(() => {
console.log('Cache berhasil ditambahkan');
})
);
});
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(keys => {
return Promise.all(keys.map(key => {
if (key !== CACHE_NAME) {
return caches.delete(key);
}
}));
})
.then(() => self.clients.claim())
);
});
self.addEventListener('fetch', event => {
if (event.request.mode === 'navigate') {
event.respondWith(
fetch(event.request)
.catch(() => caches.match(OFFLINE_URL))
.then(response => response || caches.match(OFFLINE_URL))
);
} else if (event.request.destination === 'image') {
event.respondWith(
fetch(event.request)
.catch(() => caches.match(OFFLINE_IMAGE_URL))
.then(response => response || caches.match(OFFLINE_IMAGE_URL))
);
}
});
// /**
// * code service worker only cache first page offline
// */
// // Cache offline page and offline image
// const CACHE_NAME = 'offline-cache';
// const OFFLINE_URL = 'Public/dist/pwa/offline.php';
// const OFFLINE_IMAGE_URL = 'Public/dist/pwa/offline.png';
// // Event listener for when the service worker is installed
// self.addEventListener('install', event => {
// event.waitUntil(
// // Opening the cache with the specified name
// caches.open(CACHE_NAME)
// .then(cache => {
// // Adding the offline URL and offline image URL to the cache
// return cache.addAll([OFFLINE_URL, OFFLINE_IMAGE_URL]);
// })
// .then(self.skipWaiting()) // Proceeding to the activation phase of the service worker
// );
// });
// // Event listener for when the service worker is activated
// self.addEventListener('activate', event => {
// event.waitUntil(
// // Checking the existing caches
// caches.keys().then(keys => {
// return Promise.all(keys.map(key => {
// // Deleting caches that do not match the CACHE_NAME
// if (key !== CACHE_NAME) {
// return caches.delete(key);
// }
// }));
// })
// );
// });
// // Event listener for when there is a fetch request
// self.addEventListener('fetch', event => {
// // If the request is for navigation (mode 'navigate')
// if (event.request.mode === 'navigate') {
// event.respondWith(
// // Trying to fetch content from the network
// fetch(event.request)
// // If unsuccessful, trying to find content in the cache
// .catch(() => caches.match(OFFLINE_URL))
// // Providing a response with offline content if there is no content from the network
// .then(response => response || caches.match(OFFLINE_URL))
// );
// } else if (event.request.destination === 'image') {
// event.respondWith(
// // Trying to fetch the image from the network
// fetch(event.request)
// // If unsuccessful, trying to find the image in the cache
// .catch(() => caches.match(OFFLINE_IMAGE_URL))
// // Providing a response with the offline image if there is no image from the network
// .then(response => response || caches.match(OFFLINE_IMAGE_URL))
// );
// }
// });