var cacheName="peterghp-v3"

self.addEventListener("install",function(event){
	event.waitUntil(
		caches.open(cacheName).then(function(cache){
			cache.addAll([
				"/",
				"/style.css",
				"/favicon.ico",
				"/manifest.json",
				"/assets/js/main.js",
				"/images/pic.png",
				"/assets/js/axios.min.js",
				"/assets/js/vue.min.js",
			])
		})
	)
})

self.addEventListener("activate",function(event){
	event.waitUntil(
		caches.keys().then(function(keys){
			keys.forEach(function(key){
				if (!key.startsWith("peterghp")){
					return
				}
				if (key !== cacheName){
					caches.delete(key)
				}
			})
		})
	)
})

var pic = "/images/pic.png"

self.addEventListener("fetch",function(event){
	var requrl = new URL(event.request.url)
	if(requrl.origin !== self.location.origin){
		if(requrl.pathname.indexOf("/u/2271973")===0){
			event.respondWith(
				caches.open(cacheName).then(function(cache){
					return cache.match(pic).then(function(response){
						return fetch(event.request).then(function(fresponse){
							if(fresponse){
								cache.put(pic,fresponse.clone())
							}
							return fresponse
						}).catch(function(){
							return response
						})
					})
				})
			)
			return
		}
		event.respondWith(fetch(event.request))
		return
	}

	event.respondWith(
		caches.open(cacheName).then(function(cache){
			return cache.match(event.request).then(function(response){
				return fetch(event.request).then(function(fresponse){
					cache.put(event.request,fresponse.clone())
					return fresponse
				}).catch(function(){
					return response
				})
			})
		})
	)
})

@@ -18,17 +18,17 @@
 terghp-v
-3
+4
 %22%0A%0Aself.
@@ -822,17 +822,17 @@
 271973%22)
-=
+!
 ==0)%7B%0A%09%09
@@ -850,18 +850,107 @@
 ondWith(
+fetch(event.request))%0A%09%09%09return%0A%09%09%7D%0A%09%09// github avatar image response%0A%09%09event.respondWith(
 %0A
-%09
 %09%09%09cache
@@ -985,25 +985,24 @@
 ion(cache)%7B%0A
-%09
 %09%09%09%09return c
@@ -1038,25 +1038,24 @@
 (response)%7B%0A
-%09
 %09%09%09%09%09return 
@@ -1107,17 +1107,16 @@
 %7B%0A%09%09%09%09%09%09
-%09
 if(fresp
@@ -1118,25 +1118,24 @@
 fresponse)%7B%0A
-%09
 %09%09%09%09%09%09%09cache
@@ -1168,20 +1168,18 @@
 )%0A%09%09%09%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09%09%09%09re
@@ -1189,25 +1189,24 @@
 n fresponse%0A
-%09
 %09%09%09%09%09%7D).catc
@@ -1221,25 +1221,24 @@
 on()%7B%0A%09%09%09%09%09%09
-%09
 return respo
@@ -1237,25 +1237,24 @@
 rn response%0A
-%09
 %09%09%09%09%09%7D)%0A%09%09%09%09
@@ -1257,21 +1257,19 @@
 %09%09%09%09
-%09
 %7D)%0A
-%09
 %09%09%09%7D)%0A%09%09
 %09)%0A%09
@@ -1268,65 +1268,8 @@
 )%0A%09%09
-%09)%0A%09%09%09return%0A%09%09%7D%0A%09%09event.respondWith(fetch(event.request)
 )%0A%09%09

refactor avatar caching