-
Notifications
You must be signed in to change notification settings - Fork 19
/
sw.js
143 lines (127 loc) · 4 KB
/
sw.js
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
---
layout: null
---
'use strict';
const staticCacheName = "version-{{site.time | date: '%Y%m%d%H%M%S'}}";
const pagesCacheName = 'pages-{{site.time | date: '%Y%m%d%H%M%S'}}';
const imagesCacheName = 'images';
const cacheList = [
staticCacheName,
pagesCacheName,
imagesCacheName
];
const offlinePages = [
"/",
{% for page in site.html_pages %}
'{{ page.url | relative_url }}',
{% endfor %}
{% for post in site.posts limit:3 %}
'{{ post.url | relative_url }}',
{% endfor %}
];
function updateStaticCache() {
return caches.open(staticCacheName)
}
function stashInCache(cacheName, request, response) {
caches.open(cacheName)
.then(cache => cache.put(request, response));
}
// Limit the number of items in a specified cache.
function trimCache(cacheName, maxItems) {
caches.open(cacheName)
.then(cache => {
cache.keys()
.then(keys => {
if (keys.length > maxItems) {
cache.delete(keys[0])
.then(trimCache(cacheName, maxItems));
}
});
});
}
// Remove caches whose name is no longer valid
function clearOldCaches() {
return caches.keys()
.then(keys => {
return Promise.all(keys
.filter(key => !cacheList.includes(key))
.map(key => caches.delete(key))
);
});
}
self.addEventListener('install', event => {
event.waitUntil(updateStaticCache()
.then(() => self.skipWaiting())
);
});
self.addEventListener('activate', event => {
event.waitUntil(clearOldCaches()
.then(() => self.clients.claim())
);
});
self.addEventListener('message', event => {
if (event.data.command == 'trimCaches') {
trimCache(pagesCacheName, 35);
trimCache(imagesCacheName, 20);
}
});
self.addEventListener('fetch', event => {
let request = event.request;
let url = new URL(request.url);
//Ignore non-GET requests
if (request.method !== 'GET') {
return;
}
// For HTML requests, try the network first, fall back to the cache, finally the offline page
if (request.headers.get('Accept').includes('text/html')) {
event.respondWith(
fetch(request)
.then(response => {
// NETWORK
// Stash a copy of this page in the pages cache
let copy = response.clone();
if (offlinePages.includes(url.pathname) || offlinePages.includes(url.pathname + '/')) {
stashInCache(staticCacheName, request, copy);
} else {
stashInCache(pagesCacheName, request, copy);
}
return response;
})
.catch(() => {
// CACHE or FALLBACK
return caches.match(request)
.then(response => response || caches.match('/offline/'));
})
);
return;
}
// For non-HTML requests, look in the cache first, fall back to the network
event.respondWith(
caches.match(request)
.then(response => {
// CACHE
return response || fetch(request)
.then(response => {
// NETWORK
// If the request is for an image, stash a copy of this image in the images cache
if (request.headers.get('Accept').includes('image')) {
let copy = response.clone();
stashInCache(imagesCacheName, request, copy);
}
return response;
})
.catch(() => {
// OFFLINE
// If the request is for an image, show an offline placeholder
if (request.headers.get('Accept').includes('image')) {
return new Response('<svg role="img" aria-labelledby="offline-title" viewBox="0 0 400 300" xmlns="http://www.w3.org/2000/svg"><title id="offline-title">Offline</title><g fill="none" fill-rule="evenodd"><path fill="#D8D8D8" d="M0 0h400v300H0z"/><text fill="#9B9B9B" font-family="Helvetica Neue,Arial,Helvetica,sans-serif" font-size="72" font-weight="bold"><tspan x="93" y="172">offline</tspan></text></g></svg>', {
headers: {
'Content-Type': 'image/svg+xml',
'Cache-Control': 'no-store'
}
});
}
});
})
);
});