-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice-worker.js
More file actions
38 lines (26 loc) · 997 Bytes
/
service-worker.js
File metadata and controls
38 lines (26 loc) · 997 Bytes
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
import { precacheAndRoute } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing';
import { NetworkOnly, StaleWhileRevalidate, NetworkFirst } from 'workbox-strategies';
// Precaching (manifest will be injected by the build)
precacheAndRoute(self.__WB_MANIFEST || []);
// Ignore requests to Google Analytics (don't intercept)
registerRoute(
({url}) => url.hostname === 'www.google-analytics.com',
new NetworkOnly()
);
// Cache images with a stale-while-revalidate strategy
registerRoute(
({ request }) => request.destination === 'image',
new StaleWhileRevalidate({ cacheName: 'images' })
);
// Navigation requests: Network first with fallback to cache
registerRoute(
({ request }) => request.mode === 'navigate',
new NetworkFirst({ cacheName: 'pages' })
);
// Message handler to skip waiting when new SW is available
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});