generated from dionbiancha/react-pwa-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.ts
30 lines (25 loc) · 776 Bytes
/
service-worker.ts
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
// service-worker.ts
// Adicione as importações necessárias aqui
// Configura as opções de cache
const CACHE_NAME = "my-pwa-cache";
const CACHE_VERSION = "v1";
const CACHE_URLS = ["/"];
// Instala o service worker e adiciona os recursos no cache
self.addEventListener("install", (event: ExtendableEvent) => {
event.waitUntil(
caches
.open(`${CACHE_NAME}-${CACHE_VERSION}`)
.then((cache) => cache.addAll(CACHE_URLS))
);
});
// Intercepta as solicitações de rede e retorna os recursos em cache se disponíveis
self.addEventListener("fetch", (event: FetchEvent) => {
event.respondWith(
caches.match(event.request).then((response) => {
if (response) {
return response;
}
return fetch(event.request);
})
);
});