Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
103 lines (97 loc) · 3.01 KB
/
Copy pathsw.js
File metadata and controls
103 lines (97 loc) · 3.01 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
// TaurusTech Service Worker - Offline Support
const CACHE_NAME = 'taurustech-v1';
const STATIC_ASSETS = [
'/',
'/css/styles.css',
'/js/core.js',
'/js/theme-polyfill.js',
'/js/typewriter.js',
'/js/visits.js',
'/js/projects.js',
'/js/guestbook.js',
'/partials/header.html',
'/partials/nav.html',
'/partials/footer.html',
'/images/hero.png',
'/images/under-construction.gif',
'/data/projects.json'
];
// Install event - cache static assets
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
// Cache files individually to handle missing files gracefully
return Promise.allSettled(
STATIC_ASSETS.map(url =>
cache.add(url).catch(err => {
console.warn(`Failed to cache ${url}:`, err);
return null;
})
)
);
})
.then(() => self.skipWaiting())
.catch(err => {
console.error('Service worker installation failed:', err);
})
);
});
// Activate event - clean up old caches
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames
.filter(cacheName => cacheName !== CACHE_NAME)
.map(cacheName => caches.delete(cacheName))
);
}).then(() => self.clients.claim())
);
});
// Fetch event - serve from cache, fallback to network
self.addEventListener('fetch', event => {
// Skip non-GET requests and external domains
if (event.request.method !== 'GET' ||
!event.request.url.startsWith(self.location.origin)) {
return;
}
event.respondWith(
caches.match(event.request)
.then(response => {
// Return cached version if available
if (response) {
return response;
}
// Otherwise fetch from network
return fetch(event.request).then(networkResponse => {
// Cache successful responses for future use
if (networkResponse && networkResponse.status === 200) {
const responseToCache = networkResponse.clone();
caches.open(CACHE_NAME)
.then(cache => cache.put(event.request, responseToCache))
.catch(err => console.warn('Failed to cache response:', err));
}
return networkResponse;
});
})
.catch(err => {
console.warn('Fetch failed:', err);
// Offline fallback for HTML pages
if (event.request.destination === 'document') {
return caches.match('/').catch(() => {
return new Response('Offline - Please check your connection', {
status: 503,
statusText: 'Service Unavailable',
headers: { 'Content-Type': 'text/plain' }
});
});
}
// Return a basic error response for other resources
return new Response('Resource unavailable offline', {
status: 503,
statusText: 'Service Unavailable'
});
})
);
});