From 8af3e93ca7dc5209d5f19ac384bf99d27d3b0ff4 Mon Sep 17 00:00:00 2001 From: Gavyn Caldwell Date: Fri, 13 Mar 2026 10:33:02 -0600 Subject: [PATCH] buffer streamed responses before edge caching Cache API rejects responses without Content-Length. TanStack Start streams HTML (chunked transfer), so cache.put() was silently failing. Now reads the full response body into an ArrayBuffer first. --- src/routes/-worker-entry.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/routes/-worker-entry.ts b/src/routes/-worker-entry.ts index bedd741..72aad88 100644 --- a/src/routes/-worker-entry.ts +++ b/src/routes/-worker-entry.ts @@ -348,14 +348,17 @@ export default { // Pass through to TanStack Start const response = await tanstackFetch(request); - // Store cacheable responses in edge cache + // Buffer and store cacheable responses in edge cache. + // TanStack streams HTML (no Content-Length), so we must read the + // full body before cache.put() will accept it. 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; + const body = await response.arrayBuffer(); + const headers = new Headers(response.headers); + headers.set("Cache-Control", cacheHeader); + const buffered = new Response(body, { status: 200, headers }); + cache.put(cacheKey, buffered.clone()).catch(() => {}); + return buffered; } return response;