fix: atualização da estratégia do Service Worker e feedback do botão de adicionar conhecimento

This commit is contained in:
2026-05-27 20:18:09 +00:00
parent 9e48ef6e5e
commit 24157e8906
2 changed files with 64 additions and 31 deletions
+54 -29
View File
@@ -1,4 +1,4 @@
const CACHE_NAME = 'camila-ai-v1';
const CACHE_NAME = 'camila-ai-v2';
const urlsToCache = [
'/',
'/index.html',
@@ -9,26 +9,26 @@ const urlsToCache = [
'/icon-512.png'
];
// Install event - cache assets
// Instalação - abre cache e armazena os arquivos iniciais
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('Camila AI: Cache opened');
console.log('Camila AI SW: Armazenando cache inicial');
return cache.addAll(urlsToCache);
})
.then(() => self.skipWaiting())
);
});
// Activate event - clean old caches
// Ativação - limpa caches antigos
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
console.log('Camila AI: Deleting old cache:', cacheName);
console.log('Camila AI SW: Removendo cache antigo:', cacheName);
return caches.delete(cacheName);
}
})
@@ -37,32 +37,57 @@ self.addEventListener('activate', (event) => {
);
});
// Fetch event - serve from cache, fallback to network
// Interceptador de requisições: Network-First para arquivos de código, Cache-First para mídias/assets
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then((response) => {
if (response) {
return response;
}
return fetch(event.request).then((response) => {
// Don't cache non-successful responses or non-GET requests
if (!response || response.status !== 200 || event.request.method !== 'GET') {
return response;
if (event.request.method !== 'GET') return;
const url = new URL(event.request.url);
// Ignora requisições de API e chamadas externas (não as intercepta)
if (url.pathname.startsWith('/api') || !url.origin.startsWith(self.location.origin)) {
return;
}
const isAsset = url.pathname.endsWith('.png') ||
url.pathname.endsWith('.jpg') ||
url.pathname.endsWith('.jpeg') ||
url.pathname.endsWith('.gif') ||
url.pathname.endsWith('.ico') ||
url.pathname.endsWith('.svg') ||
url.pathname.includes('/assets/');
if (isAsset) {
// Cache-First para imagens, ícones e mídias estáticas
event.respondWith(
caches.match(event.request).then((cachedResponse) => {
if (cachedResponse) return cachedResponse;
return fetch(event.request).then((networkResponse) => {
if (networkResponse && networkResponse.status === 200) {
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, networkResponse.clone());
});
}
// Clone the response
const responseToCache = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseToCache);
});
return response;
return networkResponse;
});
})
.catch(() => {
// If both cache and network fail, show offline page for HTML requests
if (event.request.headers.get('accept').includes('text/html')) {
return caches.match('/');
}
})
);
);
} else {
// Network-First para HTML, CSS, JS e manifest (garante código sempre atualizado pós-deploy)
event.respondWith(
fetch(event.request)
.then((networkResponse) => {
if (networkResponse && networkResponse.status === 200) {
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, networkResponse.clone());
});
}
return networkResponse;
})
.catch(() => {
// Se estiver offline, serve do cache
return caches.match(event.request);
})
);
}
});