cloudflare-module: 404 from env.ASSETS.fetch inherits the immutable _headers rule of its public-assets base, so browsers cache missing chunks for a year
Environment
- nitropack 2.13.4 (via nuxt 4.5.2), preset
cloudflare-module - wrangler 4.123.0, Workers Static Assets with
"binding": "ASSETS" - The relevant code is identical on
v2andmain.
Reproduction
Fresh app, no custom config:
npx nuxi@latest init repro --template minimal
cd repro
NITRO_PRESET=cloudflare-module npx nuxi buildwrangler.jsonc:
{
"name": "nitro-404-repro",
"main": ".output/server/index.mjs",
"compatibility_date": "2025-07-15",
"compatibility_flags": ["nodejs_compat"],
"assets": { "directory": ".output/public", "binding": "ASSETS" }
}npx wrangler dev --port 8806
curl -sI http://localhost:8806/_nuxt/missing.js
# HTTP/1.1 404 Not Found
# Cache-Control: public, max-age=31536000, immutableWe see the same response in production on Cloudflare Workers.
Describe the bug
A request for a file that does not exist under a public-assets base with maxAge gets a 404 that the browser may cache for a year.
- For every
publicAssetsentry withmaxAgeandfallthrough: false, nitro derives a route rulecache-control: public, max-age=<maxAge>, immutable(src/core/config/resolvers/assets.ts#L47-L53). Nuxt registers/_nuxt/this way. writeCFHeaderswrites that rule to_headersas/_nuxt/*(src/presets/cloudflare/utils.ts#L108).- The
cloudflare-moduleentry returnsenv.ASSETS.fetch(request)for any path under such a base, before h3 runs (src/presets/cloudflare/runtime/cloudflare-module.ts#L21-L22). - The asset layer applies the
_headersrule to its 404 as well, and nitro passes that response through unchanged.
A 404 with an explicit max-age is cacheable (RFC 9111 §3), so the browser keeps "this chunk does not exist" for the full lifetime. A normal reload does not revalidate it; only a hard reload does. In a Nuxt app this surfaces as Failed to fetch dynamically imported module: …/_nuxt/<hash>.js that persists until the user hard-reloads, even after the chunk is available.
Because the entry forwards these paths before h3, neither server middleware nor route rules can fix this from app code.
Additional context
Expected: a 404 from the asset layer should not carry the long-lived cache-control meant for existing hashed files. For example, the entry could set cache-control: no-store on a 404 that env.ASSETS.fetch returns.
Workaround we use: a custom nitro.options.entry that wraps the preset handler.
import nitroHandler from 'nitropack/presets/cloudflare/runtime/cloudflare-module'
const LONG_LIVED = /immutable|max-age=[1-9]/i
export default {
...nitroHandler, // keeps scheduled/email/queue/tail/trace
async fetch(request, env, ctx) {
const res = await nitroHandler.fetch(request, env, ctx)
if (res.status !== 404 || !LONG_LIVED.test(res.headers.get('cache-control') ?? '')) return res
const copy = new Response(res.body, res)
copy.headers.set('cache-control', 'no-store')
return copy
},
}Not verified:
- Why the chunk is briefly missing right after a deploy in the first place. We see the error shortly after deploys but have not measured the cause.
- Whether
cloudflare-pagesbehaves the same way.cloudflare-pages.tsuses the sameisPublicAssetURL→env.ASSETS.fetchpattern, but we have not tested it.
Source: nitrojs/nitro