forked from bokub/nopaste
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw.js
96 lines (89 loc) · 3.73 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
const VERSION = '20201201';
const PRECACHE = 'precache-' + VERSION;
const MODES = 'modes-' + VERSION;
// A regexp to detect mode files
const MODE_REGEXP = /^https:\/\/cdn\.jsdelivr.net\/npm\/codemirror@.+?\/mode\//;
// A list of local resources we always want to be cached.
const PRECACHE_URLS = [
'/',
'script.js',
'style.css',
'favicon.ico',
'https://cdn.jsdelivr.net/npm/[email protected]/src/lzma_worker.min.js',
'https://cdn.jsdelivr.net/combine/' +
'npm/[email protected]/src/lzma.min.js,' +
'npm/[email protected]/dist/slimselect.min.js,' +
'npm/clipboard@2/dist/clipboard.min.js,' +
'npm/[email protected]/dist/micromodal.min.js,' +
'npm/[email protected],' +
'npm/[email protected]/addon/mode/loadmode.min.js,' +
'npm/[email protected]/addon/mode/overlay.min.js,' +
'npm/[email protected]/addon/mode/multiplex.min.js,' +
'npm/[email protected]/addon/mode/simple.min.js,' +
'npm/[email protected]/addon/scroll/simplescrollbars.js,' +
'npm/[email protected]/mode/meta.min.js',
'https://cdn.jsdelivr.net/combine/' +
'npm/[email protected]/dist/css/bootstrap-grid.min.css,' +
'npm/[email protected]/dist/slimselect.min.css,' +
'npm/[email protected]/lib/codemirror.min.css,' +
'npm/[email protected]/addon/scroll/simplescrollbars.css,' +
'npm/[email protected]/theme/dracula.min.css,' +
'npm/[email protected]/microtip.min.css',
'https://cdn.jsdelivr.net/gh/JetBrains/[email protected]/fonts/webfonts/JetBrainsMono-Regular.woff2',
'https://cdn.jsdelivr.net/gh/JetBrains/[email protected]/fonts/ttf/JetBrainsMonoNL-Regular.ttf',
'https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu4mxK.woff2',
];
// The install handler takes care of precaching the resources we always need.
self.addEventListener('install', (event) => {
event.waitUntil(
caches
.open(PRECACHE)
.then((cache) => cache.addAll(PRECACHE_URLS))
.then(self.skipWaiting())
);
});
// The activate handler takes care of cleaning up old caches.
self.addEventListener('activate', (event) => {
const currentCaches = [PRECACHE, MODES];
event.waitUntil(
caches
.keys()
.then((cacheNames) => {
return cacheNames.filter((cacheName) => !currentCaches.includes(cacheName));
})
.then((cachesToDelete) => {
return Promise.all(
cachesToDelete.map((cacheToDelete) => {
return caches.delete(cacheToDelete);
})
);
})
.then(() => self.clients.claim())
);
});
// The fetch handler serves responses from a cache.
// If no response is found, it populates the runtime cache with the response
// from the network before returning it to the page.
self.addEventListener('fetch', (event) => {
if (!event.request.url.startsWith(self.location.origin) && !event.request.url.startsWith('https://cdn.jsdelivr.net')) {
return;
}
event.respondWith(
caches.match(event.request, { ignoreSearch: true }).then((cachedResponse) => {
if (cachedResponse) {
return cachedResponse;
}
if (!event.request.url.match(MODE_REGEXP)) {
return fetch(event.request);
}
return caches.open(MODES).then((cache) => {
return fetch(event.request).then((response) => {
// Put a copy of the response in the runtime cache.
return cache.put(event.request, response.clone()).then(() => {
return response;
});
});
});
})
);
});