-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
32 lines (28 loc) · 757 Bytes
/
service-worker.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
const cache_name="static_cache"
const Staticassets=['index.html','resources','stylesheet.css']
async function preCache(){
const cache = await caches.open(cache_name)
return cache.addAll(Staticassets);
}
self.addEventListener('install',event=>{
console.log("sw installed");
event.waitunitil(preCache())
})
self.addEventListener('activate',event=>{
console.log("sw activated")
})
async function fetchAssets(event){
try{
const response = await fetch(event.request)
return response;
}
catch(error)
{
const cache=await caches.open(cache_name)
return cache.match(event.request)
}
}
self.addEventListener('fetch',event=>{
console.log("sw fetched")
event.respondWith(fetchAssets(event))
})