-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker_reference.txt
109 lines (99 loc) · 3.25 KB
/
service-worker_reference.txt
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
'use strict';
// based on https://regino.dev/como-transformar-um-site-no-github-pages-feito-com-jekyll-em-uma-pwa/
const VERSION = "1.0.1";
const APP_CACHE_NAME = 'lb-blog-app';
const STATIC_CACHE_NAME = 'lb-blog-static';
//rotas dos arquivos estáticos
const CACHE_STATIC = [
'/assets/images/android-chrome-192x192.png',
'/assets/images/android-chrome-384x384.png',
'/assets/images/apple-touch-icon.png',
'/assets/images/kimberly-farmer-lUaaKCUANVI-unsplash.jpg',
'/assets/images/favicon-16x16.png',
'/assets/images/favicon-32x32.png',
'/assets/images/logo_blog_crop_512x512.png'
];
// routes of pages site to cache
const CACHE_APP = [
"/",
"/404.html",
"/about/",
"/tags-menu/",
"/2022/03/08/series-introduction.html",
"/tag/L&M",
"/tag/Who-am-i",
'/offline/'
];
self.addEventListener('install', function (e) {
e.waitUntil(
Promise.all([
caches.open(STATIC_CACHE_NAME),
caches.open(APP_CACHE_NAME),
self.skipWaiting()
]).then(function (storage) {
var static_cache = storage[0];
var app_cache = storage[1];
return Promise.all([
static_cache.addAll(CACHE_STATIC),
app_cache.addAll(CACHE_APP)]);
})
);
});
self.addEventListener('activate', function (e) {
e.waitUntil(
Promise.all([
self.clients.claim(),
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.map(function (cacheName) {
if (cacheName !== APP_CACHE_NAME && cacheName !== STATIC_CACHE_NAME) {
console.log('deleting', cacheName);
return caches.delete(cacheName);
}
})
);
})
])
);
});
/* this.addEventListener('fetch', function (event) {
var response;
event.respondWith(caches.match(event.request)
.then(function (match) {
return match || fetch(event.request);
}).catch(function () {
return fetch(event.request);
})
.then(function (r) {
response = r;
caches.open(APP_CACHE_NAME).then(function (cache) {
cache.put(event.request, response);
});
return response.clone();
})
);
}); */
self.addEventListener('fetch', event => {
let request = event.request;
event.respondWith(
caches.match(request)
.then(response => {
return response || fetch(request)
.then(response => {
// NETWORK
if (response && response.ok) {
let copy = response.clone();
caches.open(APP_CACHE_NAME)
.then(cache => cache.put(request, copy));
}
return response;
})
.catch(error => {
// OFFLINE
if (request.mode == 'navigate') {
return caches.match('/offline/');
}
});
})
);
});