71 lines
1.6 KiB
JavaScript
71 lines
1.6 KiB
JavaScript
const CACHE_NAME = 'lewel-cache-v1';
|
|
const urlsToCache = [
|
|
'/',
|
|
'/css/site.css',
|
|
'/images/lewel-icon.png',
|
|
'/manifest-v2.json'
|
|
];
|
|
|
|
self.addEventListener('install', event => {
|
|
event.waitUntil(
|
|
caches.open(CACHE_NAME).then(cache => {
|
|
return cache.addAll(urlsToCache);
|
|
})
|
|
);
|
|
});
|
|
|
|
/*self.addEventListener("fetch", function (event) {
|
|
const url = new URL(event.request.url);
|
|
|
|
// Hoppa över root / om du inte vill cachea den
|
|
if (url.pathname === "/") {
|
|
return;
|
|
}
|
|
|
|
// Annars cacha som vanligt
|
|
event.respondWith(
|
|
caches.match(event.request).then(function (response) {
|
|
return response || fetch(event.request);
|
|
})
|
|
);
|
|
});*/
|
|
|
|
self.addEventListener("fetch", event => {
|
|
// 🔴 Ignorera allt som inte är GET (POST, PUT, DELETE etc)
|
|
if (event.request.method !== "GET") {
|
|
return;
|
|
}
|
|
|
|
const url = new URL(event.request.url);
|
|
|
|
// Hoppa över root om du vill
|
|
if (url.pathname === "/") {
|
|
return;
|
|
}
|
|
|
|
event.respondWith(
|
|
caches.match(event.request).then(response => {
|
|
return response || fetch(event.request);
|
|
})
|
|
);
|
|
});
|
|
|
|
|
|
self.addEventListener('push', function (event) {
|
|
console.log("📨 Push event mottagen!", event);
|
|
|
|
const data = event.data ? event.data.json() : { title: "LEWEL", body: "Ny notis" };
|
|
|
|
const options = {
|
|
body: data.body,
|
|
icon: '/icons/lewel-icon.png',
|
|
badge: '/icons/lewel-icon.png'
|
|
};
|
|
|
|
event.waitUntil(
|
|
self.registration.showNotification(data.title, options)
|
|
);
|
|
});
|
|
|
|
|