2016-12-15 19:13:40 +00:00
|
|
|
/* global self */
|
|
|
|
|
|
|
|
const CACHE_NAME = 'next-prefetcher-v1'
|
2016-12-16 02:43:46 +00:00
|
|
|
const log = () => {}
|
2016-12-15 19:13:40 +00:00
|
|
|
|
|
|
|
self.addEventListener('install', () => {
|
2016-12-16 02:43:46 +00:00
|
|
|
log('Installing Next Prefetcher')
|
2016-12-15 19:13:40 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
self.addEventListener('activate', (e) => {
|
2016-12-16 02:43:46 +00:00
|
|
|
log('Activated Next Prefetcher')
|
2016-12-15 19:13:40 +00:00
|
|
|
e.waitUntil(Promise.all([
|
|
|
|
resetCache(),
|
|
|
|
notifyClients()
|
|
|
|
]))
|
|
|
|
})
|
|
|
|
|
|
|
|
self.addEventListener('fetch', (e) => {
|
|
|
|
e.respondWith(getResponse(e.request))
|
|
|
|
})
|
|
|
|
|
|
|
|
self.addEventListener('message', (e) => {
|
|
|
|
switch (e.data.action) {
|
|
|
|
case 'ADD_URL': {
|
2016-12-16 02:43:46 +00:00
|
|
|
log('CACHING ', e.data.url)
|
2016-12-15 19:13:40 +00:00
|
|
|
sendReply(e, cacheUrl(e.data.url))
|
|
|
|
break
|
|
|
|
}
|
|
|
|
case 'RESET': {
|
2016-12-16 02:43:46 +00:00
|
|
|
log('RESET')
|
2016-12-15 19:13:40 +00:00
|
|
|
sendReply(e, resetCache())
|
|
|
|
break
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
console.error('Unknown action: ' + e.data.action)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function sendReply (e, result) {
|
|
|
|
const payload = { action: 'REPLY', actionType: e.data.action, replyFor: e.data.id }
|
|
|
|
result
|
|
|
|
.then((result) => {
|
|
|
|
payload.result = result
|
|
|
|
e.source.postMessage(payload)
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
payload.error = error.message
|
|
|
|
e.source.postMessage(payload)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function cacheUrl (url) {
|
|
|
|
const req = new self.Request(url, {
|
|
|
|
mode: 'no-cors'
|
|
|
|
})
|
|
|
|
|
|
|
|
return self.caches.open(CACHE_NAME)
|
|
|
|
.then((cache) => {
|
|
|
|
return self.fetch(req)
|
|
|
|
.then((res) => cache.put(req, res))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function getResponse (req) {
|
|
|
|
return self.caches.open(CACHE_NAME)
|
|
|
|
.then((cache) => cache.match(req))
|
|
|
|
.then((res) => {
|
|
|
|
if (res) {
|
2016-12-16 02:43:46 +00:00
|
|
|
log('CACHE HIT: ' + req.url)
|
2016-12-15 19:13:40 +00:00
|
|
|
return res
|
|
|
|
} else {
|
2016-12-16 02:43:46 +00:00
|
|
|
log('CACHE MISS: ' + req.url)
|
2016-12-15 19:13:40 +00:00
|
|
|
return self.fetch(req)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetCache () {
|
|
|
|
let cache
|
|
|
|
|
|
|
|
return self.caches.open(CACHE_NAME)
|
|
|
|
.then((c) => {
|
|
|
|
cache = c
|
|
|
|
return cache.keys()
|
|
|
|
})
|
|
|
|
.then(function (items) {
|
|
|
|
const deleteAll = items.map((item) => cache.delete(item))
|
|
|
|
return Promise.all(deleteAll)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function notifyClients () {
|
|
|
|
return self.clients.claim()
|
|
|
|
.then(() => self.clients.matchAll())
|
|
|
|
.then((clients) => {
|
|
|
|
const notifyAll = clients.map((client) => {
|
|
|
|
return client.postMessage({ action: 'NEXT_PREFETCHER_ACTIVATED' })
|
|
|
|
})
|
|
|
|
return Promise.all(notifyAll)
|
|
|
|
})
|
|
|
|
}
|