-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
120 lines (110 loc) · 3.57 KB
/
sw.js
File metadata and controls
120 lines (110 loc) · 3.57 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
---
layout: null
---
// Build version: {{ site.time | date: '%Y%m%d%H%M%S' }}
importScripts('https://storage.googleapis.com/workbox-cdn/releases/7.3.0/workbox-sw.js');
const { registerRoute } = workbox.routing;
const { StaleWhileRevalidate, CacheFirst } = workbox.strategies;
const { ExpirationPlugin } = workbox.expiration;
const { CacheableResponsePlugin } = workbox.cacheableResponse;
const OFFLINE_URL = '/offline.html';
const OFFLINE_CACHE = 'offline-{{ site.time | date: "%Y%m%d%H%M%S" }}';
const ONE_YEAR = 365 * 24 * 60 * 60;
const cacheable200 = new CacheableResponsePlugin({ statuses: [200] });
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(OFFLINE_CACHE).then((cache) => cache.addAll([OFFLINE_URL, '/assets/css/application.css', '/assets/js/search.js']))
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(caches.keys().then((keys) => Promise.all(keys.filter((k) => k.startsWith('offline-') && k !== OFFLINE_CACHE).map((k) => caches.delete(k)))));
});
registerRoute(
({ request }) => request.mode === 'navigate',
async ({ request }) => fetch(request).catch(() => caches.match(OFFLINE_URL, { cacheName: OFFLINE_CACHE }))
);
registerRoute(
({ request }) => request.destination === 'style' || request.destination === 'script',
new StaleWhileRevalidate({ cacheName: 'assets', plugins: [cacheable200] })
);
registerRoute(
({ request }) => request.destination === 'font',
new CacheFirst({ cacheName: 'fonts', plugins: [cacheable200, new ExpirationPlugin({ maxEntries: 10, maxAgeSeconds: ONE_YEAR })] })
);
// Runtime caching is intentionally disabled for now.
// Keep the service worker registration to preserve PWA support.
//
// registerRoute(
// ({ request }) => request.mode === 'navigate',
// new StaleWhileRevalidate({
// cacheName: 'pages',
// plugins: [
// new CacheableResponsePlugin({ statuses: [200] }),
// new ExpirationPlugin({ maxEntries: 50 }),
// ],
// })
// );
//
// registerRoute(
// ({ request }) => request.destination === 'style',
// new StaleWhileRevalidate({
// cacheName: 'styles',
// plugins: [
// new CacheableResponsePlugin({ statuses: [200] }),
// new ExpirationPlugin({ maxEntries: 20 }),
// ],
// })
// );
//
// registerRoute(
// ({ request }) => request.destination === 'script',
// new StaleWhileRevalidate({
// cacheName: 'scripts',
// plugins: [
// new CacheableResponsePlugin({ statuses: [200] }),
// new ExpirationPlugin({ maxEntries: 20 }),
// ],
// })
// );
//
// registerRoute(
// ({ request }) => request.destination === 'image',
// new StaleWhileRevalidate({
// cacheName: 'images',
// plugins: [
// new CacheableResponsePlugin({ statuses: [200] }),
// new ExpirationPlugin({
// maxEntries: 100,
// maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days
// }),
// ],
// })
// );
//
// registerRoute(
// ({ request }) => request.destination === 'font',
// new CacheFirst({
// cacheName: 'fonts',
// plugins: [
// new CacheableResponsePlugin({ statuses: [200] }),
// new ExpirationPlugin({
// maxEntries: 10,
// maxAgeSeconds: 365 * 24 * 60 * 60, // 1 year
// }),
// ],
// })
// );
//
// registerRoute(
// ({ url }) => url.origin === '{{ site.cdn_url }}',
// new StaleWhileRevalidate({
// cacheName: 'cdn-assets',
// plugins: [
// new CacheableResponsePlugin({ statuses: [200] }),
// new ExpirationPlugin({
// maxEntries: 200,
// maxAgeSeconds: 30 * 24 * 60 * 60, // 30 days
// }),
// ],
// })
// );