From 24157e8906b8835b61c0e5960a0dfca4f9ced85c Mon Sep 17 00:00:00 2001 From: admtracksteel Date: Wed, 27 May 2026 20:18:09 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20fix:=20atualiza=C3=A7=C3=A3o=20da?= =?UTF-8?q?=20estrat=C3=A9gia=20do=20Service=20Worker=20e=20feedback=20do?= =?UTF-8?q?=20bot=C3=A3o=20de=20adicionar=20conhecimento?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/app.js | 12 ++++++-- public/sw.js | 83 +++++++++++++++++++++++++++++++++------------------ 2 files changed, 64 insertions(+), 31 deletions(-) diff --git a/public/app.js b/public/app.js index da1bd76..29975e1 100644 --- a/public/app.js +++ b/public/app.js @@ -257,11 +257,19 @@ const initApp = () => { body: JSON.stringify({ fact: input, source: 'manual' }) }); if (res.ok) { - manualKnowledgeInput.value = ''; - await loadConhecimento(); + const data = await res.json(); + if (data.added) { + manualKnowledgeInput.value = ''; + await loadConhecimento(); + } else { + alert('Esta informação já existe ou é similar a um conhecimento que a IA já possui.'); + } + } else { + alert('Erro ao salvar no servidor. Verifique sua conexão.'); } } catch (e) { console.error('Erro ao adicionar conhecimento:', e); + alert('Erro ao processar requisição.'); } }); } diff --git a/public/sw.js b/public/sw.js index 365abd5..7359b1d 100644 --- a/public/sw.js +++ b/public/sw.js @@ -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); + }) + ); + } }); \ No newline at end of file