This repository has been archived by the owner on Nov 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathsw.js
158 lines (133 loc) · 3.66 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
// @flow weak
/* eslint-disable no-console */
const DEBUG = false
// When the user navigates to your site,
// the browser tries to redownload the script file that defined the service
// worker in the background.
// If there is even a byte's difference in the service worker file compared
// to what it currently has, it considers it 'new'.
const { assets } = global.serviceWorkerOption
const CACHE_NAME = new Date().toISOString()
let assetsToCache = [...assets, './']
assetsToCache = assetsToCache.map(path => {
return new URL(path, global.location).toString()
})
// When the service worker is first added to a computer.
self.addEventListener('install', event => {
// Perform install steps.
if (DEBUG) {
console.log('[SW] Install event')
}
// Add core website files to cache during serviceworker installation.
event.waitUntil(
global.caches
.open(CACHE_NAME)
.then(cache => {
return cache.addAll(assetsToCache)
})
.then(() => {
if (DEBUG) {
console.log('Cached assets: main', assetsToCache)
}
})
.catch(error => {
console.error(error)
throw error
})
)
})
// After the install event.
self.addEventListener('activate', event => {
if (DEBUG) {
console.log('[SW] Activate event')
}
// Clean the caches
event.waitUntil(
global.caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
// Delete the caches that are not the current one.
if (cacheName.indexOf(CACHE_NAME) === 0) {
return null
}
return global.caches.delete(cacheName)
})
)
})
)
})
self.addEventListener('message', event => {
switch (event.data.action) {
case 'skipWaiting':
if (self.skipWaiting) {
self.skipWaiting()
self.clients.claim()
}
break
default:
break
}
})
self.addEventListener('fetch', event => {
const request = event.request
// Ignore not GET request.
if (request.method !== 'GET') {
if (DEBUG) {
console.log(`[SW] Ignore non GET request ${request.method}`)
}
return
}
const requestUrl = new URL(request.url)
// Ignore difference origin.
if (requestUrl.origin !== location.origin) {
if (DEBUG) {
console.log(`[SW] Ignore difference origin ${requestUrl.origin}`)
}
return
}
const resource = global.caches.match(request).then(response => {
if (response) {
if (DEBUG) {
console.log(`[SW] fetch URL ${requestUrl.href} from cache`)
}
return response
}
// Load and cache known assets.
return fetch(request)
.then(responseNetwork => {
if (!responseNetwork || !responseNetwork.ok) {
if (DEBUG) {
console.log(
`[SW] URL [${requestUrl.toString()}] wrong responseNetwork: ${
responseNetwork.status
} ${responseNetwork.type}`
)
}
return responseNetwork
}
if (DEBUG) {
console.log(`[SW] URL ${requestUrl.href} fetched`)
}
const responseCache = responseNetwork.clone()
global.caches
.open(CACHE_NAME)
.then(cache => {
return cache.put(request, responseCache)
})
.then(() => {
if (DEBUG) {
console.log(`[SW] Cache asset: ${requestUrl.href}`)
}
})
return responseNetwork
})
.catch(() => {
// User is landing on our page.
if (event.request.mode === 'navigate') {
return global.caches.match('./')
}
return null
})
})
event.respondWith(resource)
})