Update service-worker.js

This commit is contained in:
FURK4NGG
2024-09-11 14:48:48 +03:00
committed by GitHub
parent ccba711576
commit 3dfbde7164
+27 -26
View File
@@ -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 = [ const urlsToCache = [
'/', '/',
'/index.html?v=9', `/index.html?v=${CURRENT_VERSION}`,
'/not_found_page.html', '/not_found_page.html',
'/service-worker.js', '/service-worker.js',
'/img/ai.webp', '/img/ai.webp',
@@ -19,41 +22,41 @@ const urlsToCache = [
'/.htaccess.txt' '/.htaccess.txt'
]; ];
// Kullanıcının dilini kontrol et // Check the user's language
const userLang = navigator.language || navigator.userLanguage; 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') ? [ const additionalUrlsToCache = userLang.startsWith('tr') ? [
'/home.html?v=8', // Aynı dosya adı `/home.html?v=${CURRENT_VERSION}`, // Same file name with versioning
'/CSS/tr/1.css', '/CSS/tr/1.css?v=${CURRENT_VERSION}',
'/CSS/tr/2.css', '/CSS/tr/2.css?v=${CURRENT_VERSION}',
'/JAVASCRIPT/tr/1.js' '/JAVASCRIPT/tr/1.js?v=${CURRENT_VERSION}'
] : [ ] : [
'/home.html?v=8', // Aynı dosya adı `/home.html?v=${CURRENT_VERSION}`, // Same file name with versioning
'/CSS/1.css', '/CSS/1.css?v=${CURRENT_VERSION}',
'/CSS/2.css', '/CSS/2.css?v=${CURRENT_VERSION}',
'/JAVASCRIPT/1.js' '/JAVASCRIPT/1.js?v=${CURRENT_VERSION}'
]; ];
// Service Worker install event // Install event - Add files to the cache
self.addEventListener('install', event => { self.addEventListener('install', event => {
event.waitUntil( event.waitUntil(
caches.open(CACHE_NAME) caches.open(CACHE_NAME).then(cache => {
.then(cache => { console.log('Opened cache: ' + CACHE_NAME);
console.log('Opened cache');
return cache.addAll([...urlsToCache, ...additionalUrlsToCache]); return cache.addAll([...urlsToCache, ...additionalUrlsToCache]);
}) })
); );
}); });
// Service Worker activate event // Activate event - Clear old caches
self.addEventListener('activate', event => { self.addEventListener('activate', event => {
const cacheWhitelist = [CACHE_NAME];
event.waitUntil( event.waitUntil(
caches.keys().then(cacheNames => { caches.keys().then(cacheNames => {
return Promise.all( return Promise.all(
cacheNames.map(cacheName => { 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); return caches.delete(cacheName);
} }
}) })
@@ -62,17 +65,15 @@ self.addEventListener('activate', event => {
); );
}); });
// Service Worker fetch event // Fetch event - Serve cached content or fetch from network
self.addEventListener('fetch', event => { self.addEventListener('fetch', event => {
event.respondWith( event.respondWith(
caches.match(event.request) caches.match(event.request).then(response => {
.then(response => {
if (response) { if (response) {
return response; return response;
} }
return fetch(event.request)
.then(response => { return fetch(event.request).then(response => {
// Dinamik olarak önbelleğe al
if (event.request.method === 'GET' && response.status === 200) { if (event.request.method === 'GET' && response.status === 200) {
const responseClone = response.clone(); const responseClone = response.clone();
caches.open(CACHE_NAME).then(cache => { caches.open(CACHE_NAME).then(cache => {