Update service-worker.js
This commit is contained in:
+40
-39
@@ -1,7 +1,10 @@
|
||||
const CACHE_NAME = 'my-site-cache-v8';
|
||||
// Determine the cache version dynamically
|
||||
const CURRENT_VERSION = 9; // Current cache version number
|
||||
const CACHE_NAME = `my-site-cache-v${CURRENT_VERSION}`; // Dynamically named cache
|
||||
|
||||
const urlsToCache = [
|
||||
'/',
|
||||
'/index.html?v=9',
|
||||
`/index.html?v=${CURRENT_VERSION}`,
|
||||
'/not_found_page.html',
|
||||
'/service-worker.js',
|
||||
'/img/ai.webp',
|
||||
@@ -19,41 +22,41 @@ const urlsToCache = [
|
||||
'/.htaccess.txt'
|
||||
];
|
||||
|
||||
// Kullanıcının dilini kontrol et
|
||||
// Check the user's language
|
||||
const userLang = navigator.language || navigator.userLanguage;
|
||||
|
||||
// Eğer tarayıcı dili Türkçe ise /tr klasöründeki dosyaları ekle
|
||||
const additionalUrlsToCache = userLang.startsWith('tr') ? [
|
||||
'/home.html?v=8', // Aynı dosya adı
|
||||
'/CSS/tr/1.css',
|
||||
'/CSS/tr/2.css',
|
||||
'/JAVASCRIPT/tr/1.js'
|
||||
`/home.html?v=${CURRENT_VERSION}`, // Same file name with versioning
|
||||
'/CSS/tr/1.css?v=${CURRENT_VERSION}',
|
||||
'/CSS/tr/2.css?v=${CURRENT_VERSION}',
|
||||
'/JAVASCRIPT/tr/1.js?v=${CURRENT_VERSION}'
|
||||
] : [
|
||||
'/home.html?v=8', // Aynı dosya adı
|
||||
'/CSS/1.css',
|
||||
'/CSS/2.css',
|
||||
'/JAVASCRIPT/1.js'
|
||||
`/home.html?v=${CURRENT_VERSION}`, // Same file name with versioning
|
||||
'/CSS/1.css?v=${CURRENT_VERSION}',
|
||||
'/CSS/2.css?v=${CURRENT_VERSION}',
|
||||
'/JAVASCRIPT/1.js?v=${CURRENT_VERSION}'
|
||||
];
|
||||
|
||||
// Service Worker install event
|
||||
// Install event - Add files to the cache
|
||||
self.addEventListener('install', event => {
|
||||
event.waitUntil(
|
||||
caches.open(CACHE_NAME)
|
||||
.then(cache => {
|
||||
console.log('Opened cache');
|
||||
return cache.addAll([...urlsToCache, ...additionalUrlsToCache]);
|
||||
})
|
||||
caches.open(CACHE_NAME).then(cache => {
|
||||
console.log('Opened cache: ' + CACHE_NAME);
|
||||
return cache.addAll([...urlsToCache, ...additionalUrlsToCache]);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
// Service Worker activate event
|
||||
// Activate event - Clear old caches
|
||||
self.addEventListener('activate', event => {
|
||||
const cacheWhitelist = [CACHE_NAME];
|
||||
event.waitUntil(
|
||||
caches.keys().then(cacheNames => {
|
||||
return Promise.all(
|
||||
cacheNames.map(cacheName => {
|
||||
if (!cacheWhitelist.includes(cacheName)) {
|
||||
// If there's a cache with an older version, delete it
|
||||
const currentCacheNumber = parseInt(CACHE_NAME.match(/\d+/)[0], 10);
|
||||
const cacheVersionNumber = parseInt(cacheName.match(/\d+/)[0], 10);
|
||||
if (cacheVersionNumber < currentCacheNumber) {
|
||||
console.log('Deleting outdated cache:', cacheName);
|
||||
return caches.delete(cacheName);
|
||||
}
|
||||
})
|
||||
@@ -62,25 +65,23 @@ self.addEventListener('activate', event => {
|
||||
);
|
||||
});
|
||||
|
||||
// Service Worker fetch event
|
||||
// Fetch event - Serve cached content or fetch from network
|
||||
self.addEventListener('fetch', event => {
|
||||
event.respondWith(
|
||||
caches.match(event.request)
|
||||
.then(response => {
|
||||
if (response) {
|
||||
return response;
|
||||
}
|
||||
return fetch(event.request)
|
||||
.then(response => {
|
||||
// Dinamik olarak önbelleğe al
|
||||
if (event.request.method === 'GET' && response.status === 200) {
|
||||
const responseClone = response.clone();
|
||||
caches.open(CACHE_NAME).then(cache => {
|
||||
cache.put(event.request, responseClone);
|
||||
});
|
||||
}
|
||||
return response;
|
||||
caches.match(event.request).then(response => {
|
||||
if (response) {
|
||||
return response;
|
||||
}
|
||||
|
||||
return fetch(event.request).then(response => {
|
||||
if (event.request.method === 'GET' && response.status === 200) {
|
||||
const responseClone = response.clone();
|
||||
caches.open(CACHE_NAME).then(cache => {
|
||||
cache.put(event.request, responseClone);
|
||||
});
|
||||
})
|
||||
}
|
||||
return response;
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user