all repos — NoPaste @ 64d654c3e25c46c44c1eedb9dde9eb326f9dc473

Resurrected - The PussTheCat.org fork of NoPaste

sw.js (view raw)

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