-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
120 lines (111 loc) · 3.53 KB
/
sw.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
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
110
111
112
113
114
115
116
117
118
119
const addResourcesToCache = async (resources) => {
const cache = await caches.open('v1');
await cache.addAll(resources);
};
const putInCache = async (request, response) => {
const cache = await caches.open('v1');
await cache.put(request, response);
};
const cacheFirst = async ({ request, preloadResponsePromise, fallbackUrl }) => {
// First try to get the resource from the cache
const responseFromCache = await caches.match(request);
if (responseFromCache) {
return responseFromCache;
}
// Next try to use the preloaded response, if it's there
const preloadResponse = await preloadResponsePromise;
if (preloadResponse) {
console.info('using preload response', preloadResponse);
putInCache(request, preloadResponse.clone());
return preloadResponse;
}
// Next try to get the resource from the network
try {
const responseFromNetwork = await fetch(request.clone());
// response may be used only once
// we need to save clone to put one copy in cache
// and serve second one
putInCache(request, responseFromNetwork.clone());
return responseFromNetwork;
} catch (error) {
const fallbackResponse = await caches.match(fallbackUrl);
if (fallbackResponse) {
return fallbackResponse;
}
// when even the fallback response is not available,
// there is nothing we can do, but we must always
// return a Response object
return new Response('Network error happened', {
status: 408,
headers: { 'Content-Type': 'text/plain' },
});
}
};
const enableNavigationPreload = async () => {
if (self.registration.navigationPreload) {
// Enable navigation preloads!
await self.registration.navigationPreload.enable();
}
};
self.addEventListener('activate', (event) => {
event.waitUntil(enableNavigationPreload());
});
self.addEventListener('install', (event) => {
event.waitUntil(
addResourcesToCache([
'./',
'./index.html',
'./favicon.ico',
'./assets/css/bootstrap.min.css',
'./assets/css/fontawesome.min.css',
'./assets/css/adminlte.css',
'./assets/css/OverlayScrollbars.min.css',
'./assets/css/notifications.css',
'./assets/img/user2-160x160.jpg',
'./assets/img/skyline/sf-bridge.png',
'./assets/img/skyline/sf-skyline.png',
'./assets/img/skyline/sf-trees.png',
'./assets/img/skyline/sf-foreground.png',
'./assets/img/sfnight.jpg',
'./assets/img/santa.png',
'./assets/js/jquery.min.js',
'./assets/js/jquery-ui.min.js',
'./assets/js/adminlte.min.js',
'./home.html',
'./about.html',
'./contact.html',
'./doodles.html',
'./fun.html',
'./games.html',
'./tools.html',
'./work.html',
'./blog/index.html',
"./assets/img/sfnight2.jpg",
"./nav/nav.json",
"./nav/links.json",
"./nav/resume.json",
"./nav/work.json",
"./nav/blog.json",
"./nav/fun.json",
"./nav/doodles.json",
"./nav/games.json",
"./nav/resume.json",
"./nav/tools.json",
"./confetti.js",
"./assets/fontawesome-free-6.4.0-web/webfonts/fa-solid-900.woff2",
"./assets/fontawesome-free-6.4.0-web/webfonts/fa-regular-400.woff2",
"./assets/fontawesome-free-6.4.0-web/webfonts/fa-brands-400.woff2",
"./assets/css/Raleway.css",
"./main.js"
])
);
});
self.addEventListener('fetch', (event) => {
event.respondWith(
cacheFirst({
request: event.request,
preloadResponsePromise: event.preloadResponse,
fallbackUrl: './thelist.html',
})
);
});