⚡ fix: atualização da estratégia do Service Worker e feedback do botão de adicionar conhecimento
This commit is contained in:
+10
-2
@@ -257,11 +257,19 @@ const initApp = () => {
|
|||||||
body: JSON.stringify({ fact: input, source: 'manual' })
|
body: JSON.stringify({ fact: input, source: 'manual' })
|
||||||
});
|
});
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
manualKnowledgeInput.value = '';
|
const data = await res.json();
|
||||||
await loadConhecimento();
|
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) {
|
} catch (e) {
|
||||||
console.error('Erro ao adicionar conhecimento:', e);
|
console.error('Erro ao adicionar conhecimento:', e);
|
||||||
|
alert('Erro ao processar requisição.');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
+54
-29
@@ -1,4 +1,4 @@
|
|||||||
const CACHE_NAME = 'camila-ai-v1';
|
const CACHE_NAME = 'camila-ai-v2';
|
||||||
const urlsToCache = [
|
const urlsToCache = [
|
||||||
'/',
|
'/',
|
||||||
'/index.html',
|
'/index.html',
|
||||||
@@ -9,26 +9,26 @@ const urlsToCache = [
|
|||||||
'/icon-512.png'
|
'/icon-512.png'
|
||||||
];
|
];
|
||||||
|
|
||||||
// Install event - cache assets
|
// Instalação - abre cache e armazena os arquivos iniciais
|
||||||
self.addEventListener('install', (event) => {
|
self.addEventListener('install', (event) => {
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.open(CACHE_NAME)
|
caches.open(CACHE_NAME)
|
||||||
.then((cache) => {
|
.then((cache) => {
|
||||||
console.log('Camila AI: Cache opened');
|
console.log('Camila AI SW: Armazenando cache inicial');
|
||||||
return cache.addAll(urlsToCache);
|
return cache.addAll(urlsToCache);
|
||||||
})
|
})
|
||||||
.then(() => self.skipWaiting())
|
.then(() => self.skipWaiting())
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Activate event - clean old caches
|
// Ativação - limpa caches antigos
|
||||||
self.addEventListener('activate', (event) => {
|
self.addEventListener('activate', (event) => {
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
caches.keys().then((cacheNames) => {
|
caches.keys().then((cacheNames) => {
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
cacheNames.map((cacheName) => {
|
cacheNames.map((cacheName) => {
|
||||||
if (cacheName !== CACHE_NAME) {
|
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);
|
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) => {
|
self.addEventListener('fetch', (event) => {
|
||||||
event.respondWith(
|
if (event.request.method !== 'GET') return;
|
||||||
caches.match(event.request)
|
|
||||||
.then((response) => {
|
const url = new URL(event.request.url);
|
||||||
if (response) {
|
|
||||||
return response;
|
// Ignora requisições de API e chamadas externas (não as intercepta)
|
||||||
}
|
if (url.pathname.startsWith('/api') || !url.origin.startsWith(self.location.origin)) {
|
||||||
return fetch(event.request).then((response) => {
|
return;
|
||||||
// Don't cache non-successful responses or non-GET requests
|
}
|
||||||
if (!response || response.status !== 200 || event.request.method !== 'GET') {
|
|
||||||
return response;
|
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
|
return networkResponse;
|
||||||
const responseToCache = response.clone();
|
|
||||||
caches.open(CACHE_NAME).then((cache) => {
|
|
||||||
cache.put(event.request, responseToCache);
|
|
||||||
});
|
|
||||||
return response;
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch(() => {
|
);
|
||||||
// If both cache and network fail, show offline page for HTML requests
|
} else {
|
||||||
if (event.request.headers.get('accept').includes('text/html')) {
|
// Network-First para HTML, CSS, JS e manifest (garante código sempre atualizado pós-deploy)
|
||||||
return caches.match('/');
|
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);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user