File size: 3,592 Bytes
ee88cac
34bac53
ee88cac
b9af2f7
ee88cac
b9af2f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee88cac
6c4b783
 
b9af2f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Use a version that you can update with each release
const APP_VERSION = '0.1.22';
const CACHE_NAME = `my-pwa-cache-${APP_VERSION}`;

// List of files to cache
const urlsToCache = [
    '/',
    '/css/styles.css',
    '/js/AudioStretchPlayer.js',
    '/js/BeatDetector.js',
    '/js/SignalsmithStretch.mjs',
    '/apple-touch-icon.png',
    '/favicon.ico',
    '/favicon.svg',
    '/favicon-96x96.png',
    '/icon512_rounded.png',
    '/web-app-manifest-192x192.png',
    '/web-app-manifest-512x512.png',
    '/log_mel_spec.onnx',
    '/beat_model.onnx',
    '/logits_to_bars.py',
    '/claves_high.mp3',
    '/claves_low.mp3',
];


// INSTALL EVENT: Pre-cache static resources.
self.addEventListener('install', (event) => {
    console.log('Service Worker: Installed');

    // Wait until the caching is complete
    event.waitUntil(
        caches.open(CACHE_NAME)
            .then((cache) => {
                console.log('Service Worker: Caching Files');
                return cache.addAll(urlsToCache);
            })
            .then(() => self.skipWaiting()) // Force the waiting SW to become active
    );
});

// ACTIVATE EVENT: Clean up old caches.
self.addEventListener('activate', (event) => {
    console.log(' Service Worker: Activated');

    // Remove previous cached data from disk
    event.waitUntil(
        caches.keys().then((cacheNames) => {
            return Promise.all(
                cacheNames.map((cache) => {
                    if (cache !== CACHE_NAME) {
                        console.log('Service Worker: Clearing Old Cache');
                        return caches.delete(cache);
                    }
                })
            );
        })
    );
    // Allows the service worker to control the page immediately.
    return self.clients.claim();
});

// FETCH EVENT: Intercept network requests and serve cached content when available.
self.addEventListener('fetch', (event) => {
    console.log('Service Worker: Fetching', event.request.url);

    // Check if the request is made with 'http' or 'https'
    if (!(event.request.url.startsWith('http'))) {
        return;
    }

    event.respondWith(
        // Try to find the resource in the cache.
        caches.match(event.request)
            .then((response) => {
                // If found in cache, return it.
                if (response) {
                    return response;
                }

                // If not in cache, make a network request.
                return fetch(event.request)
                    .then((response) => {
                        // Check if we received a valid response
                        if (!response || response.status !== 200 || response.type !== 'basic') {
                            return response;
                        }

                        // IMPORTANT: Clone the response. A response is a stream
                        // and can only be consumed once.
                        const responseToCache = response.clone();

                        // Open the cache and add the new resource.
                        caches.open(CACHE_NAME)
                            .then((cache) => {
                                cache.put(event.request, responseToCache);
                            });

                        return response;
                    });
            }).catch(() => {
            // This is where you can provide a fallback page if both cache and network fail.
            // For example, you could return a custom offline page.
            // return caches.match('/offline.html');
        })
    );
});