-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserviceWorker.js
More file actions
58 lines (52 loc) · 1.35 KB
/
Copy pathserviceWorker.js
File metadata and controls
58 lines (52 loc) · 1.35 KB
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
// This file is used to register a service worker for offline caching
// Define the cache name
const CACHE_NAME = "file-management-cache-v1";
// Define the files to cache
const urlsToCache = [
"/",
"/index.html",
"/manifest.json",
"/logo192.png",
"/logo512.png",
"/favicon.ico",
"/static/js/bundle.js",
"/static/js/main.chunk.js",
"/static/js/0.chunk.js",
"/static/css/main.chunk.css",
"/static/media/loading.gif",
];
// Install the service worker
window.addEventListener("install", (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log("Opened cache");
return cache.addAll(urlsToCache);
})
);
});
// Activate the service worker
window.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
return null; // Add this line to return null for the else case
})
);
})
);
});
// Fetch from cache if available, otherwise fetch from network
window.addEventListener("fetch", (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
if (response) {
return response;
}
return fetch(event.request);
})
);
});