Skip to content

Commit

Permalink
network first for html
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardZaydler committed Oct 16, 2024
1 parent 413e0ed commit 37acd33
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions service-worker.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
import { precacheAndRoute } from "workbox-precaching/precacheAndRoute";
import { CacheableResponsePlugin } from "workbox-cacheable-response";
import { NetworkFirst } from "workbox-strategies";
import { registerRoute } from "workbox-routing";

precacheAndRoute(self.__WB_MANIFEST);

self.addEventListener("fetch", (event) => {
const { request } = event;
const url = request.url;
registerRoute(
({ request }) =>
request.mode === "navigate" || request.headers.get("accept")?.includes("text/html"),

console.log(
request.url,
request.mode,
request.redirect,
`includes oauth: ${url.includes("oauth")}`
);

if (url.includes("oauth")) {
const newRequest = new Request(url, {
method: "GET",
headers: request.headers,
mode: "no-cors",
credentials: request.credentials,
redirect: request.redirect,
});

event.respondWith(fetch(newRequest));
}
});
new NetworkFirst({
cacheName: "html-cache",
plugins: [
new CacheableResponsePlugin({
statuses: [0, 200],
}),
],
})
);

0 comments on commit 37acd33

Please sign in to comment.