-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
41 lines (37 loc) · 1.21 KB
/
sw.js
File metadata and controls
41 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Имя кэша для вашего PWA-приложения
var cacheName = 'piac-pwa';
// Список файлов, которые необходимо кэшировать
var filesToCache = [
'/',
'/index.html',
'/style.css',
'/js/main.js'
];
// Событие установки Service Worker: открытие кэша и добавление указанных ресурсов
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll(filesToCache);
})
);
});
// Событие fetch: перехват сетевых запросов и возврат данных из кэша при наличии
self.addEventListener('fetch', function(e) {
e.respondWith(
caches.match(e.request).then(function(response) {
return response || fetch(e.request);
})
);
});
window.onload = () => {
'use strict';
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./sw.js')
.then(() => {
console.log('Service Worker registered successfully.');
})
.catch((error) => {
console.log('Service Worker registration failed:', error);
});
}
};