diff --git a/.changeset/honest-hairs-try.md b/.changeset/honest-hairs-try.md new file mode 100644 index 000000000000..796b71cfe8ef --- /dev/null +++ b/.changeset/honest-hairs-try.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/adapter-cloudflare': patch +--- + +fix: remove redundant cloudflare worker static asset serving diff --git a/packages/adapter-cloudflare/index.js b/packages/adapter-cloudflare/index.js index 7779b3746de8..5c4cb73e47e6 100644 --- a/packages/adapter-cloudflare/index.js +++ b/packages/adapter-cloudflare/index.js @@ -1,6 +1,6 @@ -import { writeFileSync } from 'fs'; -import { posix } from 'path'; -import { fileURLToPath } from 'url'; +import { writeFileSync } from 'node:fs'; +import { posix } from 'node:path'; +import { fileURLToPath } from 'node:url'; import * as esbuild from 'esbuild'; /** @type {import('.').default} */ @@ -111,9 +111,12 @@ function get_routes_json(builder, assets) { function generate_headers(app_dir) { return ` # === START AUTOGENERATED SVELTE IMMUTABLE HEADERS === +/${app_dir}/* + X-Robots-Tag: noindex + Cache-Control: no-cache /${app_dir}/immutable/* + ! Cache-Control Cache-Control: public, immutable, max-age=31536000 - X-Robots-Tag: noindex # === END AUTOGENERATED SVELTE IMMUTABLE HEADERS === `.trimEnd(); } diff --git a/packages/adapter-cloudflare/src/worker.js b/packages/adapter-cloudflare/src/worker.js index 9c643e547f9a..545c0ebc671b 100644 --- a/packages/adapter-cloudflare/src/worker.js +++ b/packages/adapter-cloudflare/src/worker.js @@ -4,8 +4,6 @@ import * as Cache from 'worktop/cfw.cache'; const server = new Server(manifest); -const app_path = `/${manifest.appPath}/`; - /** @type {import('worktop/cfw').Module.Worker<{ ASSETS: import('worktop/cfw.durable').Durable.Object }>} */ const worker = { async fetch(req, env, context) { @@ -17,64 +15,42 @@ const worker = { if (res) return res; let { pathname } = new URL(req.url); + try { + pathname = decodeURIComponent(pathname); + } catch { + // ignore invalid URI + } - // generated files - if (pathname.startsWith(app_path)) { - res = await env.ASSETS.fetch(req); - if (!res.ok) return res; + const stripped_pathname = pathname.replace(/\/$/, ''); - const cache_control = pathname.startsWith(app_path + 'immutable/') - ? 'public, immutable, max-age=31536000' - : 'no-cache'; + // prerendered pages and /static files + let is_static_asset = false; + const filename = stripped_pathname.substring(1); + if (filename) { + is_static_asset = + manifest.assets.has(filename) || manifest.assets.has(filename + '/index.html'); + } - res = new Response(res.body, { + const location = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/'; + + if (is_static_asset || prerendered.has(pathname)) { + res = await env.ASSETS.fetch(req); + } else if (location && prerendered.has(location)) { + res = new Response('', { + status: 308, headers: { - // include original headers, minus cache-control which - // is overridden, and etag which is no longer useful - 'cache-control': cache_control, - 'content-type': res.headers.get('content-type'), - 'x-robots-tag': 'noindex' + location } }); } else { - // prerendered pages and /static files - - try { - pathname = decodeURIComponent(pathname); - } catch { - // ignore invalid URI - } - - const stripped_pathname = pathname.replace(/\/$/, ''); - - let is_static_asset = false; - const filename = stripped_pathname.substring(1); - if (filename) { - is_static_asset = - manifest.assets.has(filename) || manifest.assets.has(filename + '/index.html'); - } - - const counterpart_route = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/'; - - if (is_static_asset || prerendered.has(pathname)) { - res = await env.ASSETS.fetch(req); - } else if (counterpart_route && prerendered.has(counterpart_route)) { - res = new Response('', { - status: 308, - headers: { - location: counterpart_route - } - }); - } else { - // dynamically-generated pages - res = await server.respond(req, { - // @ts-ignore - platform: { env, context, caches }, - getClientAddress() { - return req.headers.get('cf-connecting-ip'); - } - }); - } + // dynamically-generated pages + res = await server.respond(req, { + // @ts-ignore + platform: { env, context, caches }, + getClientAddress() { + return req.headers.get('cf-connecting-ip'); + } + }); } // Writes to Cache only if allowed & specified