From 569ce4248719755fec5c01f5cd357ebf0bcddf8c Mon Sep 17 00:00:00 2001 From: Tobi Date: Tue, 9 Apr 2024 10:30:01 +0200 Subject: [PATCH] debugging PWA --- index.html | 8 +++----- manifest.json | 11 ++++++++--- service-worker.js | 49 +++++++++++++++-------------------------------- 3 files changed, 26 insertions(+), 42 deletions(-) diff --git a/index.html b/index.html index 3d12570..453ab50 100644 --- a/index.html +++ b/index.html @@ -189,15 +189,13 @@ // Hide the installation button once the app is installed document.getElementById('installButton').style.display = 'none'; - - // Hide the installation instructions once the app is installed - document.getElementById('install-instructions').style.display = 'none'; } // Show installation instructions only if app is not already installed - if (!window.matchMedia('(display-mode: standalone)').matches) { + window.addEventListener('beforeinstallprompt', (event) => { + event.preventDefault(); document.getElementById('install-instructions').style.display = 'block'; - } + }); diff --git a/manifest.json b/manifest.json index 0bcb664..39192b8 100644 --- a/manifest.json +++ b/manifest.json @@ -1,11 +1,17 @@ { - "name": "EUR-DZD", - "short_name": "EUR DZD", + "name": "EUR-DZD Calculator", + "short_name": "EUR-DZD", + "description": "Convert Euro to Algerian Dinar and vice versa.", "icons": [ { "src": "icon.png", "sizes": "192x192", "type": "image/png" + }, + { + "src": "icon.png", + "sizes": "512x512", + "type": "image/png" } ], "start_url": "/index.html", @@ -13,5 +19,4 @@ "background_color": "#f5f5f5", "theme_color": "#4CAF50" } - \ No newline at end of file diff --git a/service-worker.js b/service-worker.js index db1dba7..402b81a 100644 --- a/service-worker.js +++ b/service-worker.js @@ -1,48 +1,29 @@ const CACHE_NAME = 'eur-dzd-calculator-v1'; +const urlsToCache = [ + '/', + '/index.html', + '/icon.png', + '/manifest.json', + // Add more URLs of assets to cache here +]; -self.addEventListener('install', function(event) { +self.addEventListener('install', event => { event.waitUntil( caches.open(CACHE_NAME) - .then(function(cache) { - return cache.addAll([ - '/index.html', - '/manifest.json', - '/icon.png', - '/style.css', - '/script.js', - ]); - }) + .then(cache => cache.addAll(urlsToCache)) + .catch(error => console.error('Error caching assets:', error)) ); }); -self.addEventListener('fetch', function(event) { +self.addEventListener('fetch', event => { event.respondWith( caches.match(event.request) - .then(function(response) { + .then(response => { if (response) { - return response; + return response; // Serve cached asset if available } - - let fetchRequest = event.request.clone(); - - return fetch(fetchRequest) - .then(function(response) { - if (!response || response.status !== 200 || response.type !== 'basic') { - return response; - } - - let responseToCache = response.clone(); - - caches.open(CACHE_NAME) - .then(function(cache) { - cache.put(event.request, responseToCache); - }); - - return response; - }) - .catch(function(error) { - console.error('Error fetching and caching:', error); - }); + return fetch(event.request); // Otherwise, fetch from network }) + .catch(error => console.error('Error fetching asset:', error)) ); });