Repository navigation
Expand file tree
/
Copy pathservice-worker.js
More file actions
137 lines (123 loc) · 3.71 KB
/
Copy pathservice-worker.js
File metadata and controls
137 lines (123 loc) · 3.71 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
128
129
130
131
132
133
134
135
136
137
// Service Worker for Vineet Kishore Portfolio
// Enables offline functionality and caching
const CACHE_NAME = 'vineet-portfolio-v1.1';
const STATIC_ASSETS = [
'/',
'/index.html',
'/offline.html',
'/core/css/style.css',
'/core/css/mobile-design.css',
'/core/css/blog.css',
'/core/js/app.js',
'/core/js/portfolio-enhancements.js',
'/core/js/blog-post.js',
'/core/js/blog.js',
'/core/js/latest-articles.js',
'/core/js/terminal-enhanced.js',
'/articles/blog.html',
'/articles/blog-post.html',
'/Vineet Kishore Resume.pdf'
];
// Install event - cache static assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('[SW] Caching static assets');
return cache.addAll(STATIC_ASSETS);
})
.catch((err) => {
console.error('[SW] Cache failed:', err);
})
);
self.skipWaiting();
});
// Activate event - clean up old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => name !== CACHE_NAME)
.map((name) => {
console.log('[SW] Deleting old cache:', name);
return caches.delete(name);
})
);
})
);
self.clients.claim();
});
// Fetch event - serve from cache or network
self.addEventListener('fetch', (event) => {
const { request } = event;
// Skip non-GET requests
if (request.method !== 'GET') return;
// Skip analytics and external resources
if (request.url.includes('analytics') ||
request.url.includes('google') ||
request.url.includes('cdnjs') ||
request.url.includes('fonts')) {
return;
}
event.respondWith(
caches.match(request)
.then((response) => {
// Return cached version if available
if (response) {
// Update cache in background
fetch(request)
.then((fetchResponse) => {
caches.open(CACHE_NAME).then((cache) => {
cache.put(request, fetchResponse.clone());
});
})
.catch(() => { });
return response;
}
// Otherwise fetch from network
return fetch(request)
.then((fetchResponse) => {
// Don't cache non-successful responses
if (!fetchResponse || fetchResponse.status !== 200 || fetchResponse.type !== 'basic') {
return fetchResponse;
}
// Clone and cache the response
const responseToCache = fetchResponse.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(request, responseToCache);
});
return fetchResponse;
})
.catch(() => {
// Return offline fallback for HTML requests
if (request.headers.get('accept').includes('text/html')) {
return caches.match('/offline.html').then((response) => {
return response || caches.match('/index.html');
});
}
});
})
);
});
// Background sync for form submissions (if you add a contact form later)
self.addEventListener('sync', (event) => {
if (event.tag === 'contact-form') {
event.waitUntil(
// Retry sending form data
console.log('[SW] Background sync triggered')
);
}
});
// Push notifications (if you want to add them later)
self.addEventListener('push', (event) => {
const options = {
body: event.data.text(),
icon: '/core/assets/icon-192x192.png',
badge: '/core/assets/icon-72x72.png',
vibrate: [100, 50, 100]
};
event.waitUntil(
self.registration.showNotification('Vineet Kishore', options)
);
});