Spaces:
Sleeping
Sleeping
Commit ·
b9af2f7
1
Parent(s): 7c8abd3
add service worker
Browse files- index.html +18 -1
- sw.js +104 -0
index.html
CHANGED
|
@@ -21,7 +21,7 @@
|
|
| 21 |
|
| 22 |
<div class="app-grid">
|
| 23 |
<div class="card">
|
| 24 |
-
<h2>Upload & Loop</h2>
|
| 25 |
|
| 26 |
<div id="serverStatus" class="server-status" style="display: none;">
|
| 27 |
Checking server status...
|
|
@@ -135,6 +135,23 @@
|
|
| 135 |
</div>
|
| 136 |
|
| 137 |
<script type="module">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
import AudioStretchPlayer from '/js/AudioStretchPlayer.js';
|
| 139 |
import BeatDetector from '/js/BeatDetector.js';
|
| 140 |
|
|
|
|
| 21 |
|
| 22 |
<div class="app-grid">
|
| 23 |
<div class="card">
|
| 24 |
+
<h2>Upload & Play Loop</h2>
|
| 25 |
|
| 26 |
<div id="serverStatus" class="server-status" style="display: none;">
|
| 27 |
Checking server status...
|
|
|
|
| 135 |
</div>
|
| 136 |
|
| 137 |
<script type="module">
|
| 138 |
+
|
| 139 |
+
// Check if the browser supports service workers
|
| 140 |
+
if ('serviceWorker' in navigator) {
|
| 141 |
+
// Wait for the window to load before registering
|
| 142 |
+
window.addEventListener('load', () => {
|
| 143 |
+
navigator.serviceWorker.register('/sw.js')
|
| 144 |
+
.then((registration) => {
|
| 145 |
+
// Registration was successful
|
| 146 |
+
console.log('ServiceWorker registration successful with scope: ', registration.scope);
|
| 147 |
+
})
|
| 148 |
+
.catch((error) => {
|
| 149 |
+
// Registration failed
|
| 150 |
+
console.log('ServiceWorker registration failed: ', error);
|
| 151 |
+
});
|
| 152 |
+
});
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
import AudioStretchPlayer from '/js/AudioStretchPlayer.js';
|
| 156 |
import BeatDetector from '/js/BeatDetector.js';
|
| 157 |
|
sw.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// A version for your cache. Change this to force an update.
|
| 2 |
+
const CACHE_NAME = 'my-pwa-cache-v1';
|
| 3 |
+
|
| 4 |
+
// List of files to cache immediately during the install step.
|
| 5 |
+
const urlsToCache = [
|
| 6 |
+
'/',
|
| 7 |
+
'/css/styles.css',
|
| 8 |
+
'/js/AudioStretchPlayer.js',
|
| 9 |
+
'/js/BeatDetector.js',
|
| 10 |
+
'/js/SignalsmithStretch.mjs',
|
| 11 |
+
'/apple-touch-icon.png',
|
| 12 |
+
'/favicon.ico',
|
| 13 |
+
'/favicon.svg',
|
| 14 |
+
'/favicon-96x96.png',
|
| 15 |
+
'/icon512_rounded.png',
|
| 16 |
+
'/web-app-manifest-192x192.png',
|
| 17 |
+
'/web-app-manifest-512x512.png',
|
| 18 |
+
'/log_mel_spec.onnx',
|
| 19 |
+
'/beat_model.onnx',
|
| 20 |
+
'https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.min.js'
|
| 21 |
+
];
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
// INSTALL EVENT: Pre-cache static resources.
|
| 25 |
+
self.addEventListener('install', (event) => {
|
| 26 |
+
console.log('Service Worker: Installed');
|
| 27 |
+
|
| 28 |
+
// Wait until the caching is complete
|
| 29 |
+
event.waitUntil(
|
| 30 |
+
caches.open(CACHE_NAME)
|
| 31 |
+
.then((cache) => {
|
| 32 |
+
console.log('Service Worker: Caching Files');
|
| 33 |
+
return cache.addAll(urlsToCache);
|
| 34 |
+
})
|
| 35 |
+
.then(() => self.skipWaiting()) // Force the waiting SW to become active
|
| 36 |
+
);
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
// ACTIVATE EVENT: Clean up old caches.
|
| 40 |
+
self.addEventListener('activate', (event) => {
|
| 41 |
+
console.log(' Service Worker: Activated');
|
| 42 |
+
|
| 43 |
+
// Remove previous cached data from disk
|
| 44 |
+
event.waitUntil(
|
| 45 |
+
caches.keys().then((cacheNames) => {
|
| 46 |
+
return Promise.all(
|
| 47 |
+
cacheNames.map((cache) => {
|
| 48 |
+
if (cache !== CACHE_NAME) {
|
| 49 |
+
console.log('Service Worker: Clearing Old Cache');
|
| 50 |
+
return caches.delete(cache);
|
| 51 |
+
}
|
| 52 |
+
})
|
| 53 |
+
);
|
| 54 |
+
})
|
| 55 |
+
);
|
| 56 |
+
// Allows the service worker to control the page immediately.
|
| 57 |
+
return self.clients.claim();
|
| 58 |
+
});
|
| 59 |
+
|
| 60 |
+
// FETCH EVENT: Intercept network requests and serve cached content when available.
|
| 61 |
+
self.addEventListener('fetch', (event) => {
|
| 62 |
+
console.log('Service Worker: Fetching', event.request.url);
|
| 63 |
+
|
| 64 |
+
// Check if the request is made with 'http' or 'https'
|
| 65 |
+
if (!(event.request.url.startsWith('http'))) {
|
| 66 |
+
return;
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
event.respondWith(
|
| 70 |
+
// Try to find the resource in the cache.
|
| 71 |
+
caches.match(event.request)
|
| 72 |
+
.then((response) => {
|
| 73 |
+
// If found in cache, return it.
|
| 74 |
+
if (response) {
|
| 75 |
+
return response;
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
// If not in cache, make a network request.
|
| 79 |
+
return fetch(event.request)
|
| 80 |
+
.then((response) => {
|
| 81 |
+
// Check if we received a valid response
|
| 82 |
+
if (!response || response.status !== 200 || response.type !== 'basic') {
|
| 83 |
+
return response;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
// IMPORTANT: Clone the response. A response is a stream
|
| 87 |
+
// and can only be consumed once.
|
| 88 |
+
const responseToCache = response.clone();
|
| 89 |
+
|
| 90 |
+
// Open the cache and add the new resource.
|
| 91 |
+
caches.open(CACHE_NAME)
|
| 92 |
+
.then((cache) => {
|
| 93 |
+
cache.put(event.request, responseToCache);
|
| 94 |
+
});
|
| 95 |
+
|
| 96 |
+
return response;
|
| 97 |
+
});
|
| 98 |
+
}).catch(() => {
|
| 99 |
+
// This is where you can provide a fallback page if both cache and network fail.
|
| 100 |
+
// For example, you could return a custom offline page.
|
| 101 |
+
// return caches.match('/offline.html');
|
| 102 |
+
})
|
| 103 |
+
);
|
| 104 |
+
});
|