all repos — NoPaste @ de062991ccdf763b2091f26873047685f9c0c977

Resurrected - The PussTheCat.org fork of NoPaste

scripts/sw.js (view raw)

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