-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
88 lines (78 loc) · 3.28 KB
/
Copy pathsw.js
File metadata and controls
88 lines (78 loc) · 3.28 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
// Service Worker: simple cache-first strategy for static assets to improve repeat-visit performance
const CACHE_VERSION = 'v1';
const CORE_CACHE = `portfolio-core-${CACHE_VERSION}`;
const RUNTIME_IMAGE_CACHE = `portfolio-images-${CACHE_VERSION}`;
const CORE_ASSETS = [
'/',
'/index.html',
'/style.css',
'/script.js',
'/assets/fonts/kode_mono/Kode[wght].woff2',
'/assets/img/socials/github-142-svgrepo-com.svg',
'/assets/img/socials/linkedin-rounded-svgrepo-com.svg',
'/assets/img/socials/Twitter-X--Streamline-Bootstrap.svg'
];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CORE_CACHE).then((cache) => {
// Attempt to cache core assets; ignore failures for cross-origin items
return cache.addAll(CORE_ASSETS.map(url => new Request(url, {integrity: '', cache: 'reload'}))).catch(() => {
// Fallback: add without special request if any addAll fails
return Promise.all(CORE_ASSETS.map(u => fetch(u).then(r => cache.put(u, r.clone())).catch(() => {})));
});
})
);
self.skipWaiting();
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => Promise.all(
keys.filter(k => k !== CORE_CACHE && k !== RUNTIME_IMAGE_CACHE).map(k => caches.delete(k))
))
);
self.clients.claim();
});
// Helper: cache-first for same-origin static, runtime caching for images, network-first for navigations
self.addEventListener('fetch', (event) => {
const req = event.request;
const url = new URL(req.url);
// Don't interfere with analytics or third-party scripts (allow network)
if (url.hostname.includes('simpleanalyticscdn.com') || url.hostname.includes('unpkg.com')) {
return; // fall through to network
}
// Navigation requests (HTML) - network-first with cache fallback
if (req.mode === 'navigate') {
event.respondWith(
fetch(req).then((res) => {
// update cache
caches.open(CORE_CACHE).then(cache => cache.put('/index.html', res.clone()));
return res;
}).catch(() => caches.match('/index.html'))
);
return;
}
// Images (including cross-origin like Cloudinary) - cache-first then network & populate image cache
if (req.destination === 'image' || url.hostname.includes('res.cloudinary.com')) {
event.respondWith(
caches.open(RUNTIME_IMAGE_CACHE).then(cache => cache.match(req).then(cached => cached || fetch(req).then(netRes => { cache.put(req, netRes.clone()); return netRes; }).catch(() => cached)))
);
return;
}
// For other same-origin requests (css, js, fonts) use cache-first
if (url.origin === self.location.origin) {
event.respondWith(
caches.match(req).then(cached => cached || fetch(req).then(netRes => {
// cache built responses for future
if (req.method === 'GET') {
caches.open(CORE_CACHE).then(cache => cache.put(req, netRes.clone()));
}
return netRes;
}).catch(() => cached))
);
return;
}
// Default: let network handle it
});
// NOTE: This service worker helps repeat-visit performance locally even if server cache headers are short.
// For best results, configure your web server to set Cache-Control headers for static assets (fonts/images/css/js)
// with a long max-age and `immutable` for fingerprinted files, and a short TTL for HTML.