@@ -2329,6 +2329,54 @@ function compilePageRoutes(
23292329 lines . push ( '' ) ;
23302330}
23312331
2332+ /**
2333+ * Generate the asset fallthrough helper shared by native Void workers and
2334+ * Class A framework wrappers. If the app returns 404, check ASSETS binding
2335+ * for static files and set cache headers. Edge caching of hashed assets
2336+ * happens at the dispatch layer (see design doc 0042).
2337+ */
2338+ export function compileAssetFallbackHelper ( assetsDir = 'assets' , serveSpaFallback = false ) : string {
2339+ const spaFallbackCode = serveSpaFallback
2340+ ? ` // SPA fallback: when HTML-owning features make
2341+ // assetConfig.not_found_handling = 'none', the asset binding returns 404
2342+ // instead of falling back to index.html itself. Do that fallback here, after
2343+ // user routes/middleware have run, so auth gates and OAuth callbacks see the request first.
2344+ // Pages and SSR apps leave this disabled so intentional HTML 404s stay 404.
2345+ const accept = request.headers.get("accept") ?? "";
2346+ if (accept.includes("text/html")) {
2347+ const indexUrl = new URL("/index.html", request.url);
2348+ const indexAsset = await env.ASSETS.fetch(new Request(indexUrl, { method: request.method }));
2349+ if (indexAsset.status === 200) {
2350+ const res = new Response(indexAsset.body, {
2351+ status: 200,
2352+ headers: indexAsset.headers,
2353+ });
2354+ res.headers.set("Cache-Control", "no-store");
2355+ res.headers.set("Content-Type", "text/html; charset=utf-8");
2356+ return res;
2357+ }
2358+ }
2359+ return response;`
2360+ : ` return response;` ;
2361+
2362+ return `
2363+ async function serveWithAssets(request, env, response) {
2364+ if (response.status !== 404 || !env.ASSETS || (request.method !== "GET" && request.method !== "HEAD")) return response;
2365+ const asset = await env.ASSETS.fetch(new Request(request.url, { method: request.method, headers: request.headers }));
2366+ if (asset.status !== 404) {
2367+ const res = new Response(asset.body, asset);
2368+ if (new URL(request.url).pathname.startsWith(${ JSON . stringify ( '/' + assetsDir + '/' ) } )) {
2369+ res.headers.set("Cache-Control", "public, max-age=31536000, immutable");
2370+ } else {
2371+ res.headers.set("Cache-Control", "no-store");
2372+ }
2373+ return res;
2374+ }
2375+ ${ spaFallbackCode }
2376+ }
2377+ ` ;
2378+ }
2379+
23322380/**
23332381 * Generate the Worker entry module code.
23342382 * Re-exports the Hono app from the virtual module with an asset-serving
@@ -2438,55 +2486,7 @@ function __filterInternalBindings(env) {
24382486}
24392487` ;
24402488
2441- // Asset fallthrough helper — if the app returns 404, check ASSETS binding
2442- // for static files and set cache headers. Edge caching of hashed assets
2443- // happens at the dispatch layer (see design doc 0042).
2444- // Safe for workers without ASSETS binding (env.ASSETS check short-circuits).
2445- //
2446- // Hashed assets under assetsDir get long immutable caching (content-addressed).
2447- // Non-hashed asset responses are stamped no-store: a stale entry there would
2448- // shadow routes that are added or fixed in subsequent deploys across the edge
2449- // cache boundary, which the platform's deploy-scoped cache keys (dispatch
2450- // layer) can't reach once CF's managed edge has cached the response.
2451- const spaFallbackCode = serveSpaFallback
2452- ? ` // SPA fallback: when HTML-owning features make
2453- // assetConfig.not_found_handling = 'none', the asset binding returns 404
2454- // instead of falling back to index.html itself. Do that fallback here, after
2455- // user routes/middleware have run, so auth gates and OAuth callbacks see the request first.
2456- // Pages and SSR apps leave this disabled so intentional HTML 404s stay 404.
2457- const accept = request.headers.get("accept") ?? "";
2458- if (accept.includes("text/html")) {
2459- const indexUrl = new URL("/index.html", request.url);
2460- const indexAsset = await env.ASSETS.fetch(new Request(indexUrl, { method: request.method }));
2461- if (indexAsset.status === 200) {
2462- const res = new Response(indexAsset.body, {
2463- status: 200,
2464- headers: indexAsset.headers,
2465- });
2466- res.headers.set("Cache-Control", "no-store");
2467- res.headers.set("Content-Type", "text/html; charset=utf-8");
2468- return res;
2469- }
2470- }
2471- return response;`
2472- : ` return response;` ;
2473-
2474- const assetHelper = `
2475- async function serveWithAssets(request, env, response) {
2476- if (response.status !== 404 || !env.ASSETS || (request.method !== "GET" && request.method !== "HEAD")) return response;
2477- const asset = await env.ASSETS.fetch(new Request(request.url, { method: request.method, headers: request.headers }));
2478- if (asset.status !== 404) {
2479- const res = new Response(asset.body, asset);
2480- if (new URL(request.url).pathname.startsWith(${ JSON . stringify ( '/' + assetsDir + '/' ) } )) {
2481- res.headers.set("Cache-Control", "public, max-age=31536000, immutable");
2482- } else {
2483- res.headers.set("Cache-Control", "no-store");
2484- }
2485- return res;
2486- }
2487- ${ spaFallbackCode }
2488- }
2489- ` ;
2489+ const assetHelper = compileAssetFallbackHelper ( assetsDir , serveSpaFallback ) ;
24902490
24912491 const fetchHandler = remotePath
24922492 ? `async (request, env, ctx) => {
0 commit comments