all repos — NoPaste @ f8b75b572f8133be06a28ce9dc8d0bb346e674e5

Resurrected - The PussTheCat.org fork of NoPaste

sw.js (view raw)

 1const PRECACHE = 'precache-20201121';
 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@2.210/ttf/JetBrainsMonoNL-Regular.ttf',
30    'https://cdn.jsdelivr.net/gh/JetBrains/JetBrainsMono@2.210/fonts/webfonts/JetBrainsMono-Regular.woff2',
31    'https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Mu4mxK.woff2',
32];
33
34// The install handler takes care of precaching the resources we always need.
35self.addEventListener('install', (event) => {
36    event.waitUntil(
37        caches
38            .open(PRECACHE)
39            .then((cache) => cache.addAll(PRECACHE_URLS))
40            .then(self.skipWaiting())
41    );
42});
43
44// The activate handler takes care of cleaning up old caches.
45self.addEventListener('activate', (event) => {
46    const currentCaches = [PRECACHE, RUNTIME];
47    event.waitUntil(
48        caches
49            .keys()
50            .then((cacheNames) => {
51                return cacheNames.filter((cacheName) => !currentCaches.includes(cacheName));
52            })
53            .then((cachesToDelete) => {
54                return Promise.all(
55                    cachesToDelete.map((cacheToDelete) => {
56                        return caches.delete(cacheToDelete);
57                    })
58                );
59            })
60            .then(() => self.clients.claim())
61    );
62});
63
64// The fetch handler serves responses for same-origin resources from a cache.
65// If no response is found, it populates the runtime cache with the response
66// from the network before returning it to the page.
67self.addEventListener('fetch', (event) => {
68    if (!event.request.url.startsWith(self.location.origin) && !event.request.url.startsWith('https://cdn.jsdelivr.net')) {
69        return;
70    }
71    event.respondWith(
72        caches.match(event.request, { ignoreSearch: true }).then((cachedResponse) => {
73            if (cachedResponse) {
74                return cachedResponse;
75            }
76
77            return caches.open(RUNTIME).then((cache) => {
78                return fetch(event.request).then((response) => {
79                    // Put a copy of the response in the runtime cache.
80                    return cache.put(event.request, response.clone()).then(() => {
81                        return response;
82                    });
83                });
84            });
85        })
86    );
87});