forked from lodash/lodash.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
231 lines (210 loc) · 6.27 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
---
ignored: [
'appcache.html',
'manifest.appcache',
'robots.txt'
]
prefetch: [
'/sw.js',
'/manifest.webmanifest',
'/icons/apple-touch-180x180.png',
'/icons/favicon-32x32.png'
]
---
'use strict'
{% assign ignored = page.ignored %}
{% assign prefetch = page.prefetch %}
{% comment %}
Add conditionally ignored files.
{% endcomment %}
{% unless site.github.hostname == 'github.com' %}
{% assign ignored = ignored | push:'favicon-16x16.png' %}
{% endunless %}
{% comment %}
Add site css to prefetch.
{% endcomment %}
{% for hrefs in site.css %}
{% for href in hrefs[1] %}
{% assign prefetch = prefetch | push:href %}
{% endfor %}
{% endfor %}
{% comment %}
Add site font faces to prefetch.
{% endcomment %}
{% for family in site.font-face %}
{% for style in family[1] %}
{% for href in style[1] %}
{% assign prefetch = prefetch | push:href %}
{% endfor %}
{% endfor %}
{% endfor %}
{% comment %}
Add site js prefetch.
{% endcomment %}
{% for hrefs in site.js %}
{% for href in hrefs[1] %}
{% assign basename = href | split:'/' | last %}
{% assign ignored = ignored | push:basename %}
{% assign prefetch = prefetch | push:href %}
{% endfor %}
{% endfor %}
{% comment %}
Add static files to prefetch.
{% endcomment %}
{% for file in site.static_files %}
{% assign basename = file.path | split:'/' | last %}
{% unless ignored contains basename or file.path contains 'anchor-js' %}
{% assign prefetch = prefetch | push:file.path %}
{% endunless %}
{% endfor %}
{% comment %}
Add html pages to prefetch.
{% endcomment %}
{% for page in site.html_pages %}
{% assign href = '/' | append:page.path %}
{% assign prefetch = prefetch | push:href %}
{% endfor %}
{% comment %}
Add Lodash scripts to prefetch.
{% endcomment %}
{% for res in site.builds %}
{% assign prefetch = prefetch | push:res[1].href %}
{% endfor %}
{% comment %}
Add vendor files to prefetch.
{% endcomment %}
{% for resources in site.vendor %}
{% for res in resources[1] %}
{% assign prefetch = prefetch | push:res.href %}
{% endfor %}
{% endfor %}
{% comment %}
Cleanup prefetch.
{% endcomment %}
{% assign prefetch = prefetch | uniq | sort %}
const BUILD_REV = '{{ site.github.build_revision }}'
const prefetch = {{ prefetch | jsonify }}
.map(href => new URL(href, location))
const redirect = [/*insert_redirect*/]
.map(entry => (entry[1] = new URL(entry[1], location), entry))
const reHtml = /(?:(\/)index)?\.html$/
const reSplat = /:splat\b/
/**
* Checks if `status` is a [redirect code](https://fetch.spec.whatwg.org/#redirect-status).
*
* @param {number} status The status code to check.
* @returns {boolean} Returns `true` if `status` is a redirect code, else `false`.
*/
function isRedirect(status) {
return (
// Moved permanently.
status === 301 ||
// Moved temporarily.
status === 302 ||
// See other.
status === 303 ||
// Temporary redirect.
status === 307 ||
// Permanent redirect.
status === 308
)
}
/**
* A specialized version of `Cache#put` which caches an additional extensionless
* resource for HTML requests.
*
* @private
* @param {Object} cache The cache object
* @param {*} resource The resource key.
* @param {Object} response The response value.
* @returns {Promise} Returns a promise that resolves to `undefined`.
*/
function put(cache, resource, response) {
const isReq = resource instanceof Request
const url = new URL(isReq ? resource.url : resource)
// Add cache entry for the extensionless variant.
if (reHtml.test(url.pathname)) {
const extless = new URL(url)
extless.pathname = extless.pathname.replace(reHtml, '$1')
cache.put(new Request(extless, isReq ? resource : undefined), response.clone())
}
return cache.put(resource, response)
}
/*----------------------------------------------------------------------------*/
addEventListener('install', event =>
event.waitUntil(Promise.all([
skipWaiting(),
caches.open(BUILD_REV).then(cache =>
Promise.all(prefetch.map(uri =>
fetch(uri)
.then(response => response.ok && put(cache, uri, response))
.catch(error => console.log(`prefetch failed: ${ uri }`, error))
))
)
]))
)
addEventListener('activate', event =>
event.waitUntil(Promise.all([
clients.claim(),
// Delete old caches.
caches.keys().then(keys =>
Promise.all(keys.map(key =>
key == BUILD_REV || caches.delete(key)
))
)
]))
)
addEventListener('fetch', event => {
const { request } = event
const url = new URL(request.url)
event.respondWith(
caches.open(BUILD_REV).then(cache =>
cache.match(request).then(response => {
if (response) {
return response
}
// Detect URL redirects.
if (url.origin == location.origin) {
for (let { 0:pattern, 1:to, 2:status } of redirect) {
const match = pattern.exec(url.pathname)
const search = to.search || url.search
const splat = match ? match[1] : undefined
status = isRedirect(status) ? status : 302
if (splat !== undefined) {
to = new URL(to.pathname.replace(reSplat, splat) + search, location)
} else if (!to.search && search) {
to = new URL(to.pathname + search, location)
}
if (match) {
if (url.href != to.href) {
const response = Response.redirect(to, status)
// Repro for http://bugzil.la/1319846.
if (/fx_bug_1319846/.test(url.href)) {
put(cache, url, response.clone())
}
return response
}
break
}
}
}
// Fetch requests that weren't prefetched.
else if (!prefetch.find(({ href }) => href == url.href)) {
return fetch(request)
}
// Retry requests that failed during prefetch.
return fetch(request).then(response => {
if (response.ok) {
put(cache, request, response.clone())
}
return response
})
})
.catch(error => {
// Respond with a 400 "Bad Request" status.
console.log(`fetch failed: ${ url }`, error)
return new Response(new Blob, { 'status': 400, 'statusText': 'Bad Request' })
})
)
)
})