debugging PWA

This commit is contained in:
Tobi 2024-04-09 10:30:01 +02:00
parent fac2662460
commit 569ce42487
3 changed files with 26 additions and 42 deletions

View file

@ -189,15 +189,13 @@
// Hide the installation button once the app is installed // Hide the installation button once the app is installed
document.getElementById('installButton').style.display = 'none'; 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 // 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'; document.getElementById('install-instructions').style.display = 'block';
} });
</script> </script>
</body> </body>
</html> </html>

View file

@ -1,11 +1,17 @@
{ {
"name": "EUR-DZD", "name": "EUR-DZD Calculator",
"short_name": "EUR DZD", "short_name": "EUR-DZD",
"description": "Convert Euro to Algerian Dinar and vice versa.",
"icons": [ "icons": [
{ {
"src": "icon.png", "src": "icon.png",
"sizes": "192x192", "sizes": "192x192",
"type": "image/png" "type": "image/png"
},
{
"src": "icon.png",
"sizes": "512x512",
"type": "image/png"
} }
], ],
"start_url": "/index.html", "start_url": "/index.html",
@ -14,4 +20,3 @@
"theme_color": "#4CAF50" "theme_color": "#4CAF50"
} }

View file

@ -1,48 +1,29 @@
const CACHE_NAME = 'eur-dzd-calculator-v1'; 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( event.waitUntil(
caches.open(CACHE_NAME) caches.open(CACHE_NAME)
.then(function(cache) { .then(cache => cache.addAll(urlsToCache))
return cache.addAll([ .catch(error => console.error('Error caching assets:', error))
'/index.html',
'/manifest.json',
'/icon.png',
'/style.css',
'/script.js',
]);
})
); );
}); });
self.addEventListener('fetch', function(event) { self.addEventListener('fetch', event => {
event.respondWith( event.respondWith(
caches.match(event.request) caches.match(event.request)
.then(function(response) { .then(response => {
if (response) { if (response) {
return response; return response; // Serve cached asset if available
} }
return fetch(event.request); // Otherwise, fetch from network
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);
});
}) })
.catch(error => console.error('Error fetching asset:', error))
); );
}); });