-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
83 lines (78 loc) · 2.88 KB
/
Copy pathsw.js
File metadata and controls
83 lines (78 loc) · 2.88 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
const CACHE_NAME = 'tradeconvert-v1';
const STATIC_ASSETS = [
'/',
'/css/main.css',
'/js/converters.js',
'/assets/favicon.png',
'/assets/og-image.jpg',
'https://fonts.googleapis.com/css2?family=Playfair+Display:wght@400;600;700;900&family=Source+Serif+4:ital,opsz,wght@0,8..60,300;0,8..60,400;0,8..60,600;1,8..60,400&family=JetBrains+Mono:wght@400;500;600&display=swap'
];
// Install event: Cache critical static assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log('Opened cache');
return cache.addAll(STATIC_ASSETS);
})
);
self.skipWaiting();
});
// Activate event: Clean up old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
console.log('Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
})
);
self.clients.claim();
});
// Fetch event: Network-first for HTML pages (Stale-while-revalidate), Cache-first for static assets
self.addEventListener('fetch', (event) => {
const requestUrl = new URL(event.request.url);
// For static assets (CSS, JS, Images, Fonts), try cache first, then network
if (requestUrl.pathname.match(/\.(css|js|png|jpg|jpeg|svg|woff2?|ttf)$/) || requestUrl.hostname.includes('fonts.googleapis.com')) {
event.respondWith(
caches.match(event.request).then((response) => {
if (response) {
return response; // Return from cache
}
return fetch(event.request).then((networkResponse) => {
// Cache the new resource
if (networkResponse && networkResponse.status === 200 && networkResponse.type === 'basic') {
const responseToCache = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
}
return networkResponse;
});
})
);
} else {
// For HTML pages and API calls, use Stale-while-revalidate or Network-first
event.respondWith(
caches.match(event.request).then((cachedResponse) => {
const fetchPromise = fetch(event.request).then((networkResponse) => {
if (networkResponse && networkResponse.status === 200) {
const responseToCache = networkResponse.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
}
return networkResponse;
}).catch(() => {
// If network fails and no cache exists, just let it fail or return offline page
});
// Return cached immediately if present, but update in background
return cachedResponse || fetchPromise;
})
);
}
});