all repos — NoPaste @ 3dffb0b6a1293b955497b87048563b1a01dd974c

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