-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathsw.js
More file actions
128 lines (111 loc) · 3.84 KB
/
sw.js
File metadata and controls
128 lines (111 loc) · 3.84 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
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
importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js');
if (workbox) {
console.log(`Workbox is loaded 🎉`);
const { registerRoute } = workbox.routing;
const { CacheFirst, NetworkFirst, StaleWhileRevalidate } = workbox.strategies;
const { ExpirationPlugin } = workbox.expiration;
const { backgroundSync } = workbox;
// Cache core application assets (Styles, Scripts, Manifest)
registerRoute(
({ request }) => request.destination === 'style' ||
request.destination === 'script' ||
request.destination === 'image' ||
request.url.includes('manifest.webmanifest'),
new StaleWhileRevalidate({
cacheName: 'static-resources',
})
);
// Cache HTML pages (Navigation) - Network First
registerRoute(
({ request }) => request.mode === 'navigate' ||
request.headers.get('accept').includes('text/html'),
new NetworkFirst({
cacheName: 'pages-cache',
plugins: [
new ExpirationPlugin({
maxEntries: 50,
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
}),
],
})
);
// Cache project files from /public/ directory
registerRoute(
({ url }) => url.pathname.startsWith('/public/'),
new CacheFirst({
cacheName: 'project-files',
plugins: [
new ExpirationPlugin({
maxEntries: 100,
maxAgeSeconds: 60 * 24 * 60 * 60, // 60 Days
}),
],
})
);
// Background Sync for progress updates
const syncPlugin = new workbox.backgroundSync.BackgroundSyncPlugin('sync-progress', {
maxRetentionTime: 24 * 60 // Retry for max 24 Hours
});
// Since we are using Firestore directly in the frontend,
// background sync here would typically intercept a POST request to a custom API.
// However, for this project, we'll handle the actual DB sync via offlineService + indexedDB
// which is triggered by the SW 'sync' event or when the app comes back online.
} else {
console.log(`Workbox didn't load 😬`);
}
// Manual Background Sync Listener (if needed for custom logic)
self.addEventListener('sync', (event) => {
if (event.tag === 'sync-progress') {
event.waitUntil(syncProgressData());
}
});
async function syncProgressData() {
console.log('Background Sync: Processing queued progress data...');
// This logic is usually handled by the main thread when it comes back online,
// but can be partially handled here if we had a dedicated API endpoint.
}
const OFFLINE_PAGE = '/offline.html';
const OFFLINE_IMAGE = '/website/assets/images/logo.png';
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open('offline-cache').then((cache) => {
return cache.addAll([
OFFLINE_PAGE,
'/website/style.css',
'/website/script.js',
'/index.html',
'/manifest.webmanifest'
]);
}).then(() => self.skipWaiting())
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => !name.includes('workbox') && !name.includes('offline-cache') && !name.includes('static-resources') && !name.includes('pages-cache') && !name.includes('project-files'))
.map((name) => caches.delete(name))
);
}).then(() => self.clients.claim())
);
});
self.addEventListener('fetch', (event) => {
if (event.request.mode === 'navigate') {
event.respondWith(
fetch(event.request)
.catch(() => caches.match(OFFLINE_PAGE))
);
}
});
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
if (event.data && event.data.type === 'CACHE_PROJECT') {
const projectUrls = event.data.urls || [];
event.waitUntil(
caches.open('project-files').then((cache) => cache.addAll(projectUrls))
);
}
});