Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png" />
<!-- PAPERCLIP_FAVICON_END -->
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
<link rel="manifest" href="/site.webmanifest" />
<link rel="manifest" href="/site.webmanifest" crossorigin="use-credentials" />
<script>
(() => {
const key = "paperclip.theme";
Expand Down
33 changes: 25 additions & 8 deletions ui/public/sw.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const CACHE_NAME = "paperclip-v2";
const CACHE_NAME = "paperclip-v3";

// Auth-sensitive / self-referential paths the SW must never intercept.
const PASS_THROUGH = new Set(["/site.webmanifest", "/sw.js"]);

self.addEventListener("install", () => {
self.skipWaiting();
Expand All @@ -17,26 +20,40 @@ self.addEventListener("fetch", (event) => {
const { request } = event;
const url = new URL(request.url);

// Skip non-GET requests and API calls
if (request.method !== "GET" || url.pathname.startsWith("/api")) {
// Skip non-GET, API calls, cross-origin requests, and auth-sensitive assets.
// Returning (not calling respondWith) lets the network handle them untouched.
if (
request.method !== "GET" ||
url.origin !== self.location.origin ||
url.pathname.startsWith("/api") ||
PASS_THROUGH.has(url.pathname)
) {
return;
}

// Network-first for everything — cache is only an offline fallback
// Network-first; cache is only an offline fallback.
event.respondWith(
fetch(request)
.then((response) => {
if (response.ok && url.origin === self.location.origin) {
// Never cache redirected / opaqueredirect responses (auth challenges).
if (
response.ok &&
!response.redirected &&
response.type !== "opaqueredirect"
) {
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(request, clone));
}
return response;
})
.catch(() => {
.catch(async () => {
// Always resolve to a real Response — never undefined.
if (request.mode === "navigate") {
return caches.match("/") || new Response("Offline", { status: 503 });
const cached = await caches.match("/");
return cached || new Response("Offline", { status: 503 });
}
return caches.match(request);
const cached = await caches.match(request);
return cached || new Response("", { status: 504, statusText: "Offline" });
})
);
});
Loading