Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
93 lines (87 loc) · 3.39 KB
/
Copy pathsw.js
File metadata and controls
93 lines (87 loc) · 3.39 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
const CACHE_NAME = 'iconstudio-v4.0.5';
const ASSETS = [
'/',
'/index.html',
'/app/',
'/app/index.html',
'/style.css',
'/script.js',
'/js/components.js',
'/assets/img/base-placeholder.svg',
'/assets/favicon/favicon-96x96.png',
'/assets/favicon/favicon.svg',
'/assets/favicon/favicon.ico',
'/assets/favicon/apple-touch-icon.png',
'/assets/favicon/site.webmanifest',
'/app/manifest.webmanifest',
'/assets/favicon/web-app-manifest-192x192.png',
'/assets/favicon/web-app-manifest-512x512.png'
];
// Install Event - cache core shell assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log('[Service Worker] Caching App Shell');
return cache.addAll(ASSETS);
}).then(() => self.skipWaiting())
);
});
// Activate Event - clear old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => {
return Promise.all(
keys.map((key) => {
if (key !== CACHE_NAME) {
console.log('[Service Worker] Removing old cache:', key);
return caches.delete(key);
}
})
);
}).then(() => self.clients.claim())
);
});
// Fetch Event - network-first with cache fallback
self.addEventListener('fetch', (event) => {
// Only cache GET requests
if (event.request.method !== 'GET') return;
// Skip Cloudinary/API uploads or remote URLs that shouldn't be cached in the shell
const url = new URL(event.request.url);
if (url.hostname.includes('cloudinary.com') || url.pathname.includes('/s/')) {
return;
}
event.respondWith(
caches.match(event.request).then((cachedResponse) => {
if (cachedResponse) {
// Return cached asset but fetch update in background (stale-while-revalidate)
fetch(event.request)
.then((networkResponse) => {
if (networkResponse.status === 200) {
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, networkResponse));
}
})
.catch(() => {/* Ignore network failures in background fetch */});
return cachedResponse;
}
// Fallback to network
return fetch(event.request).then((networkResponse) => {
// Cache dynamic fonts or library assets
if (
networkResponse.status === 200 &&
(url.hostname.includes('fonts.googleapis.com') ||
url.hostname.includes('fonts.gstatic.com') ||
url.hostname.includes('unpkg.com'))
) {
const responseClone = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, responseClone));
}
return networkResponse;
}).catch(() => {
// If offline and requesting a page, return the matching shell for that section
if (event.request.mode === 'navigate') {
return url.pathname.startsWith('/app/') ? caches.match('/app/') : caches.match('/');
}
});
})
);
});