-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
175 lines (157 loc) · 4.96 KB
/
Copy pathsw.js
File metadata and controls
175 lines (157 loc) · 4.96 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Service Worker for Alexander Tibbets website
* Provides offline capabilities and performance improvements
*/
const STATIC_CACHE = 'static-v1c2f215f';
const CRITICAL_ASSETS = [
'/',
'/index.html',
'/profiles.html',
'/links.html',
'/style.css?v=72dbd309',
'/assets/js/common.js?v=78c0437b',
'/assets/favicon.ico?v=assets1',
'/assets/images/favicon.svg?v=assets1',
'/assets/images/icon-192.png?v=assets1',
'/assets/images/icon-512.png?v=assets1',
'/assets/images/apple-touch-icon.png?v=assets1'
];
const STATIC_ASSETS = [...CRITICAL_ASSETS];
// Regex for identifying static assets that should use cache-first strategy
const STATIC_ASSETS_RE = /\.(?:css|js|ico|png|svg)$|\/assets\//;
/**
* Install event - cache static assets
*/
self.addEventListener('install', event => {
event.waitUntil(
caches.open(STATIC_CACHE)
.then(async cache => {
console.log('Caching static assets');
try {
await cache.addAll(STATIC_ASSETS);
} catch (error) {
console.error('Asset caching failed:', error);
throw error;
}
})
);
self.skipWaiting();
});
/**
* Activate event - clean up old caches
*/
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys()
.then(cacheNames => {
return Promise.all(
cacheNames.filter(cacheName => {
return cacheName !== STATIC_CACHE;
}).map(cacheName => {
console.log('Deleting old cache:', cacheName);
return caches.delete(cacheName);
})
);
})
.then(() => {
console.log('Service worker activated');
return self.clients.claim();
})
);
});
/**
* Fetch event - intercept requests and serve from cache
*/
self.addEventListener('fetch', event => {
const { request } = event;
// Skip non-GET requests
if (request.method !== 'GET') return;
const url = new URL(request.url);
// Skip non-HTTP(S) requests
if (!url.protocol.startsWith('http')) return;
// Handle different types of requests
if (url.pathname === '/' || url.pathname.endsWith('.html')) {
// HTML pages: stale-while-revalidate strategy for instant load
event.respondWith(staleWhileRevalidate(event, request, STATIC_CACHE));
} else if (STATIC_ASSETS_RE.test(url.pathname)) {
// Static assets, CSS/JS: cache first, then network
event.respondWith(cacheFirst(request, STATIC_CACHE));
} else {
// Other requests: network first, then cache
event.respondWith(networkFirst(request, STATIC_CACHE));
}
});
/**
* Cache first strategy - check cache first, then network
*/
async function cacheFirst(request, cacheName) {
try {
const cachedResponse = await caches.match(request);
if (cachedResponse) {
return cachedResponse;
}
const networkResponse = await fetch(request);
if (networkResponse.ok) {
const cache = await caches.open(cacheName);
cache.put(request, networkResponse.clone());
}
return networkResponse;
} catch (error) {
console.log('Cache first strategy failed:', error);
return new Response('Resource not available', { status: 503 });
}
}
/**
* Network first strategy - check network first, then cache
*/
async function networkFirst(request, cacheName) {
try {
const networkResponse = await fetch(request);
if (networkResponse.ok) {
const cache = await caches.open(cacheName);
cache.put(request, networkResponse.clone());
}
return networkResponse;
} catch (error) {
console.log('Network first strategy failed, trying cache:', error);
const cachedResponse = await caches.match(request);
if (cachedResponse) {
return cachedResponse;
}
return new Response('Offline content not available', { status: 503 });
}
}
/**
* Stale-while-revalidate strategy - serve from cache, then update from network
*/
function staleWhileRevalidate(event, request, cacheName) {
// Start network request immediately (parallel)
const networkFetch = fetch(request).then(async response => {
if (response.ok) {
const cache = await caches.open(cacheName);
await cache.put(request, response.clone());
}
return response;
}).catch(err => {
console.log('SWR background fetch failed', err);
});
// Call event.waitUntil synchronously within the main event dispatch loop
event.waitUntil(networkFetch);
// Return the promise handling the cached/network response
return respondSWR(request, cacheName, networkFetch);
}
async function respondSWR(request, cacheName, networkFetch) {
const cache = await caches.open(cacheName);
const cachedResponse = await cache.match(request);
if (cachedResponse) {
return cachedResponse;
}
try {
const response = await networkFetch;
if (response) return response;
return new Response('Offline content not available', { status: 503 });
} catch (error) {
console.log('SWR network fallback failed:', error);
return new Response('Offline content not available', { status: 503 });
}
}