-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
114 lines (101 loc) · 3.06 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
/* eslint-env serviceworker */
var CACHE_KEY = getCacheKey()
var ASSETS = process.env.ASSET_LIST
var IS_DEV = process.env.NODE_ENV === 'development'
self.addEventListener('install', function oninstall (event) {
event.waitUntil(
caches
.open(CACHE_KEY)
.then((cache) => cache.addAll(['/'].concat(ASSETS)))
.then(() => self.skipWaiting())
)
})
self.addEventListener('activate', function onactivate (event) {
event.waitUntil(clear().then(function () {
if (!self.registration.navigationPreload) return self.clients.claim()
// enable navigation preloads
return self.registration.navigationPreload.enable().then(function () {
return self.clients.claim()
})
}))
})
self.addEventListener('fetch', function onfetch (event) {
var req = event.request
var url = new self.URL(req.url)
var sameOrigin = self.location.origin === url.origin
var acceptHTML = req.headers.get('accept').includes('text/html')
event.respondWith(
caches.open(CACHE_KEY).then(function (cache) {
return cache.match(req).then(function (cached) {
if (req.cache === 'only-if-cached' && req.mode !== 'same-origin') {
return cached
}
if (!IS_DEV && cached && ASSETS.includes(url.pathname)) {
return cached
}
if (event.preloadResponse) {
return event.preloadResponse.then(function (response) {
return response || self.fetch(req)
}).then(onresponse).catch(onerror)
}
return self.fetch(req).then(onresponse).catch(onerror)
// handle network response
// Response -> Response
function onresponse (response) {
if (response.ok && req.method.toUpperCase() === 'GET') {
return cache.put(req, response.clone()).then(() => response)
}
return response
}
// handle fetch error
// Response -> Response
function onerror (err) {
if (cached) return cached
if (sameOrigin && acceptHTML) return render()
return err
}
})
})
)
})
function render () {
var script = process.env.ASSET_LIST.find(function (url) {
return /\/(?:\w+\.)?bundle\.js/.test(url)
})
var style = process.env.ASSET_LIST.find(function (url) {
return /\/(?:\w+\.)?bundle\.css/.test(url)
})
var doc = `
<!doctype html>
<html>
<head>
<script>window.initialState = { offline: true }</script>
<script src="${script}" defer></script>
<link rel="stylesheet" href="${style}">
</head>
<body></body>
</html>
`
return new self.Response(doc, {
status: 503,
headers: { 'Content-Type': 'text/html' }
})
}
// clear application cache
// () -> Promise
function clear () {
return caches.keys().then(function (keys) {
return Promise.all(
keys.filter((key) => key !== CACHE_KEY).map((key) => caches.delete(key))
)
})
}
// get application cache key
// () -> str
function getCacheKey () {
if (process.env.SOURCE_VERSION) {
return process.env.SOURCE_VERSION
} else {
return process.env.npm_package_version
}
}