From b25fb0073e2ad6f6f6f3fbb0da12db12b243eb78 Mon Sep 17 00:00:00 2001 From: Shantur Rathore Date: Thu, 22 Jan 2026 18:05:01 +0000 Subject: [PATCH] fix(cloudflare): serve version.json as static asset Avoid Workers billing for /version.json by removing worker-first routing and generating static _headers rules during manifest build. --- .../cloudflare/scripts/build-manifest.mjs | 8 +++++++ packages/cloudflare/src/index.ts | 22 +------------------ packages/cloudflare/wrangler.toml | 1 - 3 files changed, 9 insertions(+), 22 deletions(-) diff --git a/packages/cloudflare/scripts/build-manifest.mjs b/packages/cloudflare/scripts/build-manifest.mjs index 582bc34c..945e2af9 100644 --- a/packages/cloudflare/scripts/build-manifest.mjs +++ b/packages/cloudflare/scripts/build-manifest.mjs @@ -72,4 +72,12 @@ const manifest = { fs.mkdirSync(distDir, { recursive: true }) fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2) + "\n", "utf-8") +const headersPath = path.join(distDir, "_headers") +fs.writeFileSync( + headersPath, + "/version.json\n Cache-Control: no-cache\n Content-Type: application/json; charset=utf-8\n", + "utf-8", +) + console.log(`Wrote ${manifestPath}`) +console.log(`Wrote ${headersPath}`) diff --git a/packages/cloudflare/src/index.ts b/packages/cloudflare/src/index.ts index 46831ae9..fad560cf 100644 --- a/packages/cloudflare/src/index.ts +++ b/packages/cloudflare/src/index.ts @@ -2,28 +2,8 @@ export interface Env { ASSETS: { fetch: (request: Request) => Promise } } -function withHeader(response: Response, key: string, value: string): Response { - const headers = new Headers(response.headers) - headers.set(key, value) - return new Response(response.body, { - status: response.status, - statusText: response.statusText, - headers, - }) -} - export default { async fetch(request: Request, env: Env): Promise { - const url = new URL(request.url) - - if (url.pathname === "/version.json") { - const assetResponse = await env.ASSETS.fetch(request) - - // Ensure this stays fresh; the server uses it on startup. - const withCache = withHeader(assetResponse, "Cache-Control", "no-cache") - return withHeader(withCache, "Content-Type", "application/json; charset=utf-8") - } - - return new Response("Not found", { status: 404 }) + return env.ASSETS.fetch(request) }, } diff --git a/packages/cloudflare/wrangler.toml b/packages/cloudflare/wrangler.toml index fa9d7f2f..e9e6e3b0 100644 --- a/packages/cloudflare/wrangler.toml +++ b/packages/cloudflare/wrangler.toml @@ -12,4 +12,3 @@ custom_domain = true directory = "./dist" binding = "ASSETS" not_found_handling = "404-page" -run_worker_first = ["/version.json"]