sw.js (view raw)
1const PRECACHE = 'precache-20201021';
2const RUNTIME = 'runtime';
3
4// A list of local resources we always want to be cached.
5const PRECACHE_URLS = [
6 '/',
7 'script.js',
8 'style.css',
9 'https://cdn.jsdelivr.net/npm/lzma@2.3.2/src/lzma_worker.min.js',
10 'https://cdn.jsdelivr.net/combine/' +
11 'npm/lzma@2.3.2/src/lzma.min.js,' +
12 'npm/slim-select@1.25.0/dist/slimselect.min.js,' +
13 'npm/clipboard@2/dist/clipboard.min.js,' +
14 'npm/micromodal@0.4.6/dist/micromodal.min.js,' +
15 'npm/codemirror@5.58.1,' +
16 'npm/codemirror@5.58.1/addon/mode/loadmode.min.js,' +
17 'npm/codemirror@5.58.1/addon/mode/overlay.min.js,' +
18 'npm/codemirror@5.58.1/addon/mode/multiplex.min.js,' +
19 'npm/codemirror@5.58.1/addon/mode/simple.min.js,' +
20 'npm/codemirror@5.58.1/addon/scroll/simplescrollbars.js,' +
21 'npm/codemirror@5.58.1/mode/meta.min.js',
22 'https://cdn.jsdelivr.net/combine/' +
23 'npm/bootstrap@4.4.1/dist/css/bootstrap-grid.min.css,' +
24 'npm/slim-select@1.25.0/dist/slimselect.min.css,' +
25 'npm/codemirror@5.58.1/lib/codemirror.min.css,' +
26 'npm/codemirror@5.58.1/addon/scroll/simplescrollbars.css,' +
27 'npm/codemirror@5.58.1/theme/dracula.min.css,' +
28 'npm/microtip@0.2.2/microtip.min.css',
29 'https://cdn.jsdelivr.net/gh/JetBrains/JetBrainsMono/web/woff2/JetBrainsMono-Regular.woff2',
30 'https://cdn.jsdelivr.net/gh/JetBrains/JetBrainsMono/web/woff/JetBrainsMono-Regular.woff',
31 'https://cdn.jsdelivr.net/gh/JetBrains/JetBrainsMono/ttf/JetBrainsMono-Regular.ttf',
32 'https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu4mxK.woff2',
33];
34
35// The install handler takes care of precaching the resources we always need.
36self.addEventListener('install', (event) => {
37 event.waitUntil(
38 caches
39 .open(PRECACHE)
40 .then((cache) => cache.addAll(PRECACHE_URLS))
41 .then(self.skipWaiting())
42 );
43});
44
45// The activate handler takes care of cleaning up old caches.
46self.addEventListener('activate', (event) => {
47 const currentCaches = [PRECACHE, RUNTIME];
48 event.waitUntil(
49 caches
50 .keys()
51 .then((cacheNames) => {
52 return cacheNames.filter((cacheName) => !currentCaches.includes(cacheName));
53 })
54 .then((cachesToDelete) => {
55 return Promise.all(
56 cachesToDelete.map((cacheToDelete) => {
57 return caches.delete(cacheToDelete);
58 })
59 );
60 })
61 .then(() => self.clients.claim())
62 );
63});
64
65// The fetch handler serves responses for same-origin resources from a cache.
66// If no response is found, it populates the runtime cache with the response
67// from the network before returning it to the page.
68self.addEventListener('fetch', (event) => {
69 if (!event.request.url.startsWith(self.location.origin) && !event.request.url.startsWith('https://cdn.jsdelivr.net')) {
70 return;
71 }
72 event.respondWith(
73 caches.match(event.request, { ignoreSearch: true }).then((cachedResponse) => {
74 if (cachedResponse) {
75 return cachedResponse;
76 }
77
78 return caches.open(RUNTIME).then((cache) => {
79 return fetch(event.request).then((response) => {
80 // Put a copy of the response in the runtime cache.
81 return cache.put(event.request, response.clone()).then(() => {
82 return response;
83 });
84 });
85 });
86 })
87 );
88});