From ba0f392532b8af6f47dcad38eebdf06a20b1a047 Mon Sep 17 00:00:00 2001 From: abdullathedruid Date: Thu, 26 Dec 2024 00:41:01 +0000 Subject: [PATCH] feat: update manifest --- priv/static/manifest.json | 3 +- priv/static/sw.js | 72 +++++++++++++-------------------------- 2 files changed, 25 insertions(+), 50 deletions(-) diff --git a/priv/static/manifest.json b/priv/static/manifest.json index 153dc83..e66a72e 100644 --- a/priv/static/manifest.json +++ b/priv/static/manifest.json @@ -2,8 +2,9 @@ "name": "GymLive", "short_name": "GymLive", "description": "Track your workouts and progress in real-time", - "start_url": "/workouts", + "id": "/", "scope": "/", + "start_url": "/workouts", "display": "standalone", "orientation": "portrait", "background_color": "#ffffff", diff --git a/priv/static/sw.js b/priv/static/sw.js index 3f2719f..8968c7a 100644 --- a/priv/static/sw.js +++ b/priv/static/sw.js @@ -1,55 +1,29 @@ -const CACHE_NAME = 'gym-live-v1'; -const URLS_TO_CACHE = [ - '/', - '/manifest.json', - '/assets/app.css', - '/assets/app.js', - '/images/icons/icon-72x72.png', - '/images/icons/icon-96x96.png', - '/images/icons/icon-128x128.png', - '/images/icons/icon-144x144.png', - '/images/icons/icon-152x152.png', - '/images/icons/icon-192x192.png', - '/images/icons/icon-384x384.png', - '/images/icons/icon-512x512.png' -]; +const sw = self; +const config = { + debug: false, +} -self.addEventListener('install', (event) => { - event.waitUntil( - caches.open(CACHE_NAME) - .then((cache) => { - return cache.addAll(URLS_TO_CACHE); - }) - ); -}); +const { debug } = config; -self.addEventListener('fetch', (event) => { - event.respondWith( - caches.match(event.request) - .then((response) => { - // Cache hit - return response - if (response) { - return response; - } +sw.addEventListener('install', handleInstall); - return fetch(event.request).then( - (response) => { - // Check if we received a valid response - if (!response || response.status !== 200 || response.type !== 'basic') { - return response; - } +function handleInstall(event) { + debug && console.log('[Service Worker] Installed.'); +} - // Clone the response - const responseToCache = response.clone(); +sw.addEventListener('fetch', handleFetch); - caches.open(CACHE_NAME) - .then((cache) => { - cache.put(event.request, responseToCache); - }); +function handleFetch(event) { + // Ignore non-GET requests + if (event.request.method.toUpperCase() !== 'GET') { return; } - return response; - } - ); - }) - ); -}); \ No newline at end of file + // Ignore requests from Chrome extensions + if (event.request.url.startsWith("chrome-extension://")) { return; } + + const url = new URL(event.request.url); + // Ignore LiveReloader - only requested in dev + if (url.pathname === "/phoenix/live_reload/frame") { return; } + + debug && console.log('[Service Worker] Handling fetch:', event.request.url); + event.respondWith(fetch(event.request)); +}