123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- const CACHE_NAME = "1.2.3";
- const urlsToCache = [
- "/",
- "/manifest.json",
- "/images/favicon-32x32.png",
- "/images/favicon-16x16.png",
- "/apple-touch-icon.png",
- "/android-chrome-192x192.png",
- "/android-chrome-512x512.png",
- ];
- // Install event - cache resources
- self.addEventListener("install", (event) => {
- self.skipWaiting(); // 🔥 force install
- event.waitUntil(
- caches.open(CACHE_NAME).then((cache) => {
- return cache.addAll(urlsToCache);
- }),
- );
- });
- // Fetch event - network first with cache fallback
- self.addEventListener("fetch", (event) => {
- event.respondWith(
- fetch(event.request)
- .then((response) => {
- // If we get a valid response, clone it and update the cache
- if (response && response.status === 200) {
- const responseClone = response.clone();
- caches.open(CACHE_NAME).then((cache) => {
- cache.put(event.request, responseClone);
- });
- }
- return response;
- })
- .catch(() => {
- // If network fails, try to serve from cache
- return caches.match(event.request).then((cachedResponse) => {
- if (cachedResponse) {
- return cachedResponse;
- }
- // If no cache available, return a custom offline page or error
- if (event.request.destination === "document") {
- return new Response("Application hors ligne - Veuillez vérifier votre connexion internet", {
- status: 503,
- statusText: "Service Unavailable",
- headers: new Headers({
- "Content-Type": "text/html; charset=utf-8",
- }),
- });
- }
- });
- }),
- );
- });
- // Activate event - clean up old caches
- self.addEventListener("activate", (event) => {
- event.waitUntil(
- caches.keys().then((cacheNames) => {
- return Promise.all(
- cacheNames.map((cacheName) => {
- if (cacheName !== CACHE_NAME) {
- return caches.delete(cacheName);
- }
- }),
- );
- }),
- );
- self.clients.claim();
- });
|