-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserviceWorker.js
More file actions
80 lines (68 loc) · 2.42 KB
/
serviceWorker.js
File metadata and controls
80 lines (68 loc) · 2.42 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
* All of this JS code is to make sure that the pages are available even if there is no network connection
* We can specify pages now - at the 'Add any critical files you want cached immediately' point
* But we have added JS that will do this automatically for use as people open pages
* This is a better approach so that only the pages that the user is interested in will be pre-loaded.
*/
// Name of the cache
const CACHE_NAME = 'my-DECO7140-cache-v1';
// List of files to pre-cache (optional, only the essential ones)
const urlsToPreCache = [
'/', // Cache the home page
'/index.html', // Main page
// Add any critical files you want cached immediately
];
// Install event - pre-cache only essential files
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log('Opened cache');
return cache.addAll(urlsToPreCache);
})
);
});
// Activate event - clean up old caches if necessary
self.addEventListener('activate', (event) => {
const cacheWhitelist = [CACHE_NAME];
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (!cacheWhitelist.includes(cacheName)) {
return caches.delete(cacheName);
}
})
);
})
);
});
// Fetch event - serve cached content and dynamically cache new content
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
// Serve the cached response if found
if (response) {
return response;
}
// Clone the request to fetch and cache the new file
const fetchRequest = event.request.clone();
// Only cache valid HTTP/HTTPS requests
if (event.request.url.startsWith('http')) {
return fetch(fetchRequest).then((response) => {
// If the response is valid, cache it dynamically
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
const responseToCache = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
return response;
});
} else {
// For unsupported schemes like 'chrome-extension://', just fetch without caching
return fetch(event.request);
}
})
);
});