1 |
|
2 |
|
3 |
|
4 |
|
5 | var VERSION = "2018-09-17T09:42:42.594Z"
|
6 | , cacheList = [
|
7 | VERSION
|
8 | ]
|
9 | , isFileRe = /\/[-\.\w]+\.\w+$/
|
10 |
|
11 | function log() {
|
12 |
|
13 | }
|
14 |
|
15 | self.addEventListener("install", function(event) {
|
16 | log("install", VERSION)
|
17 | event.waitUntil(
|
18 | fetch("cache.manifest", { cache: "no-store" })
|
19 | .then(function(res) {
|
20 | return res.text().then(function(text) {
|
21 | return caches.open(VERSION)
|
22 | .then(function(cache) {
|
23 | var files = text.match(/^(?![#*]|CACHE MANIFEST|\w+:)[^\n\r]+/gm)
|
24 | log("cache.addAll", files)
|
25 | return cache.addAll(files)
|
26 | })
|
27 | })
|
28 | })
|
29 | .then(self.skipWaiting)
|
30 | )
|
31 | })
|
32 |
|
33 | self.addEventListener("activate", function(event) {
|
34 | log("activate", VERSION)
|
35 | event.waitUntil(
|
36 | caches.keys()
|
37 | .then(function(keys) {
|
38 | return Promise.all(keys.map(function(key) {
|
39 | if (cacheList.indexOf(key) < 0) {
|
40 | log("caches.delete", key)
|
41 | return caches.delete(key)
|
42 | }
|
43 | }))
|
44 | })
|
45 | )
|
46 | })
|
47 |
|
48 |
|
49 | self.addEventListener("fetch", function(event) {
|
50 | var url = event.request.url.slice(self.registration.scope.length).split("?")[0] || "index.html"
|
51 |
|
52 | if (
|
53 | event.request.method === "GET" &&
|
54 | isFileRe.test(url)
|
55 | ) event.respondWith(
|
56 | caches.open(VERSION)
|
57 | .then(function(cache) {
|
58 | return cache.match(url, { ignoreSearch: true })
|
59 | .then(function(response) {
|
60 | if (!response) log("MISS", url)
|
61 | return response || fetch(event.request)
|
62 | })
|
63 | })
|
64 | )
|
65 | })
|
66 |
|
67 | self.addEventListener("message", function(event) {
|
68 | var op = event.data.op || event.data
|
69 | if (op === "update") {
|
70 |
|
71 | }
|
72 | })
|
73 |
|
74 |
|
75 |
|
76 |
|