-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsw.js
77 lines (63 loc) · 1.98 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
// Service Worker
importScripts('/js/cache-polyfill.js')
const CACHE_NAME = 'blog-smarchal'
var CACHED_URLS = [
'/',
'/css/main.css',
'/js/app.js',
'/js/jekyll-search.min.js',
'/images/logo.png',
'/fonts/sourcecodepro-light-webfont.woff',
'/fonts/SourceSansPro-ExtraLight.woff',
'/search.json',
'/manifest.json'
]
// Open cache on install
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME).then(cache => {
const request = async () => {
var response = await fetch('/search.json')
var posts = await response.json()
for (var post in posts) {
CACHED_URLS.push(post.url)
}
}
request()
return cache.addAll(CACHED_URLS)
})
)
})
// Cache and update with stale-while-revalidate policy
self.addEventListener('fetch', event => {
const { request } = event
// Prevent Chrome Developer Tools error:
// Failed to execute 'fetch' on 'ServiceWorkerGlobalScope': 'only-if-cached' can be set only with 'same-origin' mode
if (request.cache === 'only-if-cached' && request.mode !== 'same-origin') {
return
}
event.respondWith(async function () {
const cache = await caches.open(CACHE_NAME)
const cachedResponsePromise = await cache.match(request)
const networkResponsePromise = fetch(request)
if (request.url.startsWith(self.location.origin)) {
event.waitUntil(async function () {
const networkResponse = await networkResponsePromise
await cache.put(request, networkResponse.clone())
}())
}
return cachedResponsePromise || networkResponsePromise
}())
})
// Clean up caches other than current.
self.addEventListener('activate', event => {
event.waitUntil(async function () {
const cacheNames = await caches.keys()
await Promise.all(
cacheNames.filter(cacheName => {
const deleteThisCache = cacheName !== CACHE_NAME
return deleteThisCache
}).map(cacheName => caches.delete(cacheName))
)
}())
})