Spaces:
Running
Running
Update service-worker.js
Browse files- service-worker.js +40 -11
service-worker.js
CHANGED
|
@@ -1,20 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
self.addEventListener('install', (event) => {
|
| 2 |
event.waitUntil(
|
| 3 |
-
caches.open(
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
'/index.html',
|
| 7 |
-
'/style.css',
|
| 8 |
-
'/icon.png' // Добавьте все необходимые ресурсы
|
| 9 |
-
]);
|
| 10 |
})
|
| 11 |
);
|
|
|
|
| 12 |
});
|
| 13 |
|
| 14 |
-
self.addEventListener('
|
| 15 |
-
event.
|
| 16 |
-
caches.
|
| 17 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
})
|
| 19 |
);
|
|
|
|
| 20 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const CACHE_NAME = 'my-site-cache-v2';
|
| 2 |
+
const URLS_TO_CACHE = [
|
| 3 |
+
'/',
|
| 4 |
+
'/index.html',
|
| 5 |
+
'/style.css',
|
| 6 |
+
'/icon.png'
|
| 7 |
+
];
|
| 8 |
+
|
| 9 |
self.addEventListener('install', (event) => {
|
| 10 |
event.waitUntil(
|
| 11 |
+
caches.open(CACHE_NAME).then((cache) => {
|
| 12 |
+
console.log('Opened cache');
|
| 13 |
+
return cache.addAll(URLS_TO_CACHE);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
})
|
| 15 |
);
|
| 16 |
+
self.skipWaiting(); // Применяет новый SW сразу
|
| 17 |
});
|
| 18 |
|
| 19 |
+
self.addEventListener('activate', (event) => {
|
| 20 |
+
event.waitUntil(
|
| 21 |
+
caches.keys().then((cacheNames) => {
|
| 22 |
+
return Promise.all(
|
| 23 |
+
cacheNames.map((cacheName) => {
|
| 24 |
+
if (cacheName !== CACHE_NAME) {
|
| 25 |
+
console.log('Deleting old cache:', cacheName);
|
| 26 |
+
return caches.delete(cacheName);
|
| 27 |
+
}
|
| 28 |
+
})
|
| 29 |
+
);
|
| 30 |
})
|
| 31 |
);
|
| 32 |
+
self.clients.claim(); // Обновляет SW без перезапуска
|
| 33 |
});
|
| 34 |
+
|
| 35 |
+
self.addEventListener('fetch', (event) => {
|
| 36 |
+
if (event.request.mode === 'navigate') {
|
| 37 |
+
event.respondWith(
|
| 38 |
+
caches.match('/index.html').then((response) => {
|
| 39 |
+
return response || fetch(event.request);
|
| 40 |
+
})
|
| 41 |
+
);
|
| 42 |
+
} else {
|
| 43 |
+
event.respondWith(
|
| 44 |
+
caches.match(event.request).then((response) => {
|
| 45 |
+
return response || fetch(event.request);
|
| 46 |
+
})
|
| 47 |
+
);
|
| 48 |
+
}
|
| 49 |
+
});
|