Skip to content

Commit

Permalink
Don't cache requests for private asset URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeseast committed Jan 7, 2025
1 parent 065504d commit 8cd1208
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/service-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const sw = self as unknown as ServiceWorkerGlobalScope;

import { build, files, version } from "$service-worker";
import { DC_BASE } from "@/config/config.js";

// Create a unique cache name for this deployment
const CACHE = `cache-${version}`;
Expand All @@ -15,6 +16,9 @@ const ASSETS = [
...files, // everything in `static`
];

// API endpoint for private assets, which shouldn't be cached
const PRIVATE_PREFIX = new URL("/files", DC_BASE).href;

sw.addEventListener("install", (event) => {
// Create a new cache and add all files to it
async function addFilesToCache() {
Expand All @@ -41,6 +45,9 @@ sw.addEventListener("fetch", (event) => {
if (event.request.method !== "GET" || !event.request.url.startsWith("http"))
return;

// ignore requests for private assets
if (event.request.url.startsWith(PRIVATE_PREFIX)) return;

async function respond() {
const url = new URL(event.request.url);
const cache = await caches.open(CACHE);
Expand Down

0 comments on commit 8cd1208

Please sign in to comment.