-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathrouter.js
32 lines (27 loc) · 921 Bytes
/
router.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
/* global self */
self.importScripts('./localforage-1.2.6.min.js')
self.addEventListener('install', function (event) {
console.log('Installing!')
})
self.addEventListener('fetch', function (event) {
var request = event.request.url
var scope = self.registration.scope
var method = event.request.method
if (method !== 'GET' || request.indexOf(scope) !== 0) {
console.log('Request', request, 'is not part of scope', scope)
console.log('Or method', method, 'is not get')
return null
}
var path = request.substring(scope.length)
console.log('Fetch request for:', path)
if (path === 'peerweb/status') {
event.respondWith(new self.Response('', { status: 234, statusText: 'intercepting' }))
} else {
event.respondWith(self.localforage.getItem(path).then(function (resp) {
if (resp) {
return self.fetch(resp)
}
return self.fetch(event.request)
}))
}
})