Skip to content

Commit

Permalink
Dont crash service worker on network failures
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels committed Sep 5, 2024
1 parent e642b45 commit 91ac291
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion nextjs-end/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ lib/firebase/config.js
public/auth-service-worker.js
.next/
.firebase/
node_modules/
node_modules/
9 changes: 7 additions & 2 deletions nextjs-end/auth-service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ let getAuthIdToken = DEFAULT_GET_AUTH_ID_TOKEN;
self.addEventListener("fetch", (event) => {
const { origin, pathname } = new URL(event.request.url);
if (origin !== self.location.origin) return;
if (pathname.includes(".")) return;
if (pathname.startsWith('/_next/')) return;
// Ignore resources with an extension—this skips css, images, fonts, json, etc.
if (!pathname.startsWith("/api/") && pathname.includes(".")) return;
event.respondWith(fetchWithFirebaseHeaders(event.request));
});

Expand All @@ -59,5 +61,8 @@ async function fetchWithFirebaseHeaders(request) {
headers.append("Authorization", `Bearer ${authIdToken}`);
request = new Request(request, { headers });
}
return await fetch(request);
return await fetch(request).catch((reason) => {
console.error(reason);
return new Response("Fail.", { status: 500, headers: { "content-type": "text/html" } });
});
}

0 comments on commit 91ac291

Please sign in to comment.