-
Notifications
You must be signed in to change notification settings - Fork 3
/
sw.js
79 lines (72 loc) · 2.28 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
const cacheName = 'KB-Cache';
const staticAssets = [
// outer
'/',
'/index.html',
'/timeline.html',
'/work.html',
'/blog.html',
'/resume.html',
// css
'https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css',
'https://unpkg.com/aos@next/dist/aos.css',
'/style.css',
// Font
'https://kit.fontawesome.com/9b54928ef1.js',
// js
'https://code.jquery.com/jquery-3.5.1.slim.min.js',
'https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js',
'https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js',
'https://unpkg.com/[email protected]/typer.js',
'https://unpkg.com/aos@next/dist/aos.js',
// assets
'/assets/images/circle-cropped copy.png',
// Add other two favicon if you need just uncomment below two.
// '/assets/images/favicon-16x16.png',
// '/assets/images/favicon-32x32.png',
'/assets/images/undraw_Hello_qnas.svg',
'/assets/images/projects/Techify Mockup.png',
'/assets/images/projects/Quizzler.gif',
'/assets/images/projects/Personal Portfolio Mockup.png',
'/assets/images/projects/EggTimer.gif',
'/assets/images/projects/Explorer Mockup.png',
'/assets/images/projects/Xylophone.gif',
]
// Service worker install event
self.addEventListener('install', async e => {
const cache = await caches.open(cacheName);
await cache.addAll(staticAssets);
return self.skipWaiting();
});
// Service Worker is activated
self.addEventListener('activate', e => {
self.clients.claim();
});
// fetch event
self.addEventListener('fetch', async e => {
const req = e.request;
const url = new URL(req.url);
if (url.origin == location.origin) {
e.respondWith(cacheFirst(req));
} else {
e.respondWith(networkAndCache(req));
}
});
// cache first matching cache
async function cacheFirst(req) {
const cache = await caches.open(cacheName);
const cached = await cache.match(req);
return cached || fetch(req);
}
// fetching new cache
async function networkAndCache(req) {
const cache = await caches.open(cacheName);
try {
const fresh = await fetch(req);
await cache.put(req, fresh.clone());
return fresh;
} catch (e) {
const cached = await cache.match(req);
return cached;
}
}