rotas no back
This commit is contained in:
25
web/sw.js
25
web/sw.js
@@ -1,4 +1,4 @@
|
||||
const CACHE = "initpage-v4";
|
||||
const CACHE = "initpage-v6";
|
||||
const ASSETS = [
|
||||
"./",
|
||||
"./index.html",
|
||||
@@ -20,12 +20,35 @@ self.addEventListener("install", (event) => {
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(
|
||||
caches.keys().then((keys) => Promise.all(keys.filter((k) => k !== CACHE).map((k) => caches.delete(k))))
|
||||
.then(() => caches.open(CACHE))
|
||||
.then((cache) => cache.keys())
|
||||
.then((requests) => Promise.all(requests.filter((request) => {
|
||||
const url = new URL(request.url);
|
||||
return /\/api(?:\/|$)/.test(url.pathname);
|
||||
}).map((request) => caches.open(CACHE).then((cache) => cache.delete(request))))
|
||||
);
|
||||
self.clients.claim();
|
||||
});
|
||||
|
||||
self.addEventListener("fetch", (event) => {
|
||||
if (event.request.method !== "GET") return;
|
||||
const requestURL = new URL(event.request.url);
|
||||
// Respostas autenticadas da API nunca devem entrar no cache compartilhado do SW.
|
||||
if (requestURL.origin === self.location.origin && /\/api(?:\/|$)/.test(requestURL.pathname)) return;
|
||||
|
||||
// Consulte o backend em cada navegação para não reaproveitar a página inicial
|
||||
// que o servidor pode ter enviado como fallback para uma rota de site estático.
|
||||
if (event.request.mode === "navigate") {
|
||||
const appHome = new URL("./", self.registration.scope);
|
||||
event.respondWith(
|
||||
fetch(event.request).catch(async () => {
|
||||
const cachedHome = await caches.match(appHome.href);
|
||||
return cachedHome || Response.error();
|
||||
})
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
event.respondWith(
|
||||
caches.match(event.request).then((cached) => {
|
||||
const fetchPromise = fetch(event.request).then((response) => {
|
||||
|
||||
Reference in New Issue
Block a user