const CACHE_NAME = 'm-auto-v2';
const urlsToCache = [
'assets/css/style.css',
'assets/images/logo.png',
'https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.rtl.min.css',
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css',
'https://fonts.googleapis.com/css2?family=Cairo:wght@300;400;700;900&display=swap'
];
// Install event
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
// Fetch event
self.addEventListener('fetch', function(event) {
const url = new URL(event.request.url);
// استثناء صفحات PHP والطلبات الديناميكية بالكامل من التخزين المؤقت
// وجعلها تجلب دائماً من السيرفر مباشرة
if (
event.request.mode === 'navigate' ||
url.pathname.endsWith('.php') ||
url.pathname === '/' ||
url.pathname.endsWith('/')
) {
event.respondWith(
fetch(event.request)
.catch(function() {
return caches.match(event.request);
})
);
} else {
// الملفات الثابتة (CSS, JS, الصور) تستخدم التخزين المؤقت أولاً
event.respondWith(
caches.match(event.request)
.then(function(response) {
return response || fetch(event.request);
})
);
}
});
// Activate event
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (cacheName !== CACHE_NAME) {
console.log('Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
})
);
});