This commit is contained in:
cpu
2025-03-28 05:50:28 +01:00
parent d1ad962ec3
commit 2e47461f34
5 changed files with 15 additions and 306 deletions

79
sw.js
View File

@@ -8,7 +8,6 @@ const CACHE_FILES = [
'/index.html',
'/app.js',
'/audio.js',
'/deeplinks.js',
'/styles.css',
'/manifest.json',
'/icons/android-chrome-192x192.png',
@@ -18,9 +17,6 @@ const CACHE_FILES = [
'/icons/favicon-16x16.png'
];
// Valid deep link actions
const VALID_ACTIONS = ['start', 'pause', 'toggle', 'nextplayer', 'reset'];
// Install event - Cache files
self.addEventListener('install', event => {
console.log('[ServiceWorker] Install');
@@ -56,57 +52,6 @@ self.addEventListener('activate', event => {
);
});
// Fetch event - Serve from cache, fallback to network
self.addEventListener('fetch', event => {
console.log('[ServiceWorker] Fetch', event.request.url);
// For navigation requests that include our deep link parameters,
// skip the cache and go straight to network
if (event.request.mode === 'navigate') {
const url = new URL(event.request.url);
// Check if request has action parameter or hash
if (url.searchParams.has('action') || url.hash.includes('action=')) {
console.log('[ServiceWorker] Processing deep link navigation');
// Verify the action is valid
const action = url.searchParams.get('action') ||
new URLSearchParams(url.hash.substring(1)).get('action');
if (action && VALID_ACTIONS.includes(action)) {
console.log('[ServiceWorker] Valid action found:', action);
// For navigation requests with valid actions, let the request go through
// so our app can handle the deep link
return;
}
}
}
event.respondWith(
caches.match(event.request)
.then(response => {
return response || fetch(event.request)
.then(res => {
// Check if we should cache this response
if (shouldCacheResponse(event.request, res)) {
return caches.open(CACHE_NAME)
.then(cache => {
console.log('[ServiceWorker] Caching new resource:', event.request.url);
cache.put(event.request, res.clone());
return res;
});
}
return res;
});
})
.catch(error => {
console.log('[ServiceWorker] Fetch failed; returning offline page', error);
// You could return a custom offline page here
})
);
});
// Helper function to determine if a response should be cached
function shouldCacheResponse(request, response) {
// Only cache GET requests
@@ -124,30 +69,6 @@ function shouldCacheResponse(request, response) {
return true;
}
// Handle deep links from other apps (including Flic)
self.addEventListener('message', event => {
if (event.data && event.data.type === 'PROCESS_ACTION') {
const action = event.data.action;
console.log('[ServiceWorker] Received action message:', action);
// Validate the action
if (!VALID_ACTIONS.includes(action)) {
console.warn('[ServiceWorker] Invalid action received:', action);
return;
}
// Broadcast the action to all clients
self.clients.matchAll().then(clients => {
clients.forEach(client => {
client.postMessage({
type: 'ACTION',
action: action
});
});
});
}
});
self.addEventListener('push', event => {
console.log('[ServiceWorker] Push received');