Skip to content
Merged
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
47 changes: 32 additions & 15 deletions src/routes/-worker-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,36 +309,53 @@ async function handleIconProxy(request: Request): Promise<Response> {

// ─── Worker Entry ───────────────────────────────────────────────────

const cache = (caches as unknown as { default: Cache }).default;

export default {
async fetch(request: Request, _env: Env): Promise<Response> {
const url = new URL(request.url);
const { pathname } = url;

// robots.txt
if (pathname === "/robots.txt") {
return robotsTxt();
}

// Sitemaps
const sitemapHandler = sitemapHandlers[pathname];
if (sitemapHandler) {
return sitemapHandler();
// robots.txt + sitemaps — serve from edge cache
if (pathname === "/robots.txt" || sitemapHandlers[pathname]) {
const cacheKey = new Request(url.toString(), { method: "GET" });
const cached = await cache.match(cacheKey);
if (cached) return cached;

const response =
pathname === "/robots.txt"
? robotsTxt()
: await sitemapHandlers[pathname]();
cache.put(cacheKey, response.clone()).catch(() => {});
return response;
}

// Icon proxy
if (pathname === "/icon") {
return handleIconProxy(request);
}

// Determine if this route is cacheable
const cacheHeader = getCacheHeader(pathname);

// Check edge cache for cacheable GET requests
if (cacheHeader && request.method === "GET") {
const cacheKey = new Request(url.toString(), { method: "GET" });
const cached = await cache.match(cacheKey);
if (cached) return cached;
}

// Pass through to TanStack Start
const response = await tanstackFetch(request);

// Apply cache headers
const cacheHeader = getCacheHeader(pathname);
if (cacheHeader && response.status === 200) {
const newResponse = new Response(response.body, response);
newResponse.headers.set("Cache-Control", cacheHeader);
return newResponse;
// Store cacheable responses in edge cache
if (cacheHeader && response.status === 200 && request.method === "GET") {
const cacheKey = new Request(url.toString(), { method: "GET" });
const cacheable = new Response(response.body, response);
cacheable.headers.set("Cache-Control", cacheHeader);
// Fire-and-forget cache write
cache.put(cacheKey, cacheable.clone()).catch(() => {});
return cacheable;
}

return response;
Expand Down
Loading