-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
104 lines (96 loc) · 3.7 KB
/
sw.js
File metadata and controls
104 lines (96 loc) · 3.7 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
// Service Worker for Image Gallery
const VERSION = '1.0.0';
const MEDIA_CACHE_NAME = `image-gallery-media-v${VERSION}`;
const STATIC_CACHE_NAME = `image-gallery-static-v${VERSION}`;
const BASE_PATH = "##path##";
self.addEventListener('install', (event) => {
console.log('Service Worker installing.');
event.waitUntil(
Promise.all([
self.skipWaiting(),
caches.open(STATIC_CACHE_NAME).then((cache) => {
return cache.addAll([
`/${BASE_PATH}/gallery.html`,
`/${BASE_PATH}/tailwind.css`,
`/${BASE_PATH}/collections.json`,
]).catch((err) => {
console.log('Caching initial assets failed');
});
}),
])
);
});
self.addEventListener('activate', (event) => {
console.log('Service Worker activating.');
event.waitUntil(
Promise.all([
self.clients.claim(),
caches.keys().then((keys) => {
return Promise.all(
keys.filter((key) => {
return key.startsWith('image-gallery-') &&
key !== MEDIA_CACHE_NAME &&
key !== STATIC_CACHE_NAME;
}).map((key) => {
console.log('Deleting old cache:', key);
return caches.delete(key);
})
);
}),
])
);
});
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
// Handle images
if (url.pathname.match(/\.(jpg|jpeg|png|gif|webp)$/i)) {
event.respondWith(
caches.open(MEDIA_CACHE_NAME).then((cache) => {
return cache.match(event.request).then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse;
}
return fetch(event.request).then((networkResponse) => {
if (networkResponse.ok) {
cache.put(event.request, networkResponse.clone());
}
return networkResponse;
}).catch(() => {
return new Response('Image not available offline', {
status: 404,
statusText: 'Not Found'
});
});
});
})
);
return;
}
// Handle HTML and JSON files - Stale While Revalidate
if (url.pathname.endsWith('gallery.html') || url.pathname.endsWith('.json')) {
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(STATIC_CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
}
return networkResponse;
}).catch((error) => {
console.log('Network fetch failed, falling back to cache');
if (cachedResponse) {
return cachedResponse;
}
return new Response(null, {
status: 404,
statusText: 'Not Found'
});
});
return cachedResponse || fetchPromise;
})
);
return;
}
});