Skip to content

Dev middleware returns HTML for imported .json modules when Sec-Fetch-Dest is missing (json not in ASSET_EXT_RE) #4433

Closed

Description

@daveycodez

Environment

  • Nitro: 3.0.260610-beta (also present on main: src/build/vite/dev.ts#L24)
  • Vite: 8.1.4
  • Framework: @solidjs/[email protected] (bug is framework-agnostic — any nitro/vite app)
  • Node 24 / macOS; reproduced in iOS Safari and desktop Chrome via a *.local origin, and with plain curl

Describe the bug

In dev, a browser request for an imported .json module (/src/data.json?import) is answered with the app's SSR'd HTML instead of the transformed JS module — but only when the request carries no Sec-Fetch-Dest header. The browser then rejects it ("Expected a JavaScript-or-Wasm module script but the server responded with a MIME type of text/html"), the dynamic import of any route module that depends on the JSON fails, and client-side navigation to that route renders a blank page.

Root cause is in nitroDevMiddlewarePre (src/build/vite/dev.ts):

  1. The asset check first consults Sec-Fetch-Dest. Browsers only send Sec-Fetch-* on potentially-trustworthy origins — http://localhost qualifies, but http://my-machine.local and LAN IPs do not, so the header is absent exactly when devs test on a phone or another machine on the LAN.

  2. The fallback is the ASSET_EXT_RE extension allowlist, and json is missing from it:

    const ASSET_EXT_RE =
      /^(?:[jt]sx?|mjs|cjs|css|s[ac]ss|less|styl|vue|svelte|astro|mdx?|map|wasm|png|jpe?g|gif|svg|webp|avif|ico|bmp|woff2?|ttf|otf|eot|mp[34]|webm|wav|ogg|m4a)$/i;

So isAsset is false, the request is routed to the Nitro dev app, and the SSR catch-all returns HTML.

The failure mode is confusing in a full app: the first (SSR) page load works, and everything works on localhost — it only breaks on client-side navigation from a non-localhost origin, which looks like a framework or HMR bug rather than a dev-middleware routing issue.

Same header-fallback class as #4201 and #4177; this one is specifically the json gap in ASSET_EXT_RE.

Reproduction

Any nitro/vite app with a JSON file under src/. The dev-middleware routing can be shown with curl alone (no importer needed):

# create any json file
echo '{"a":1}' > src/data.json

# simulating localhost (header present) — correct: 200 text/javascript
curl -s -o /dev/null -w "%{http_code} %{content_type}\n" \
  -H "Sec-Fetch-Dest: script" "http://localhost:3000/src/data.json?import"
# -> 200 text/javascript

# simulating LAN / *.local / iOS Safari (no Sec-Fetch-* headers) — bug: HTML
curl -s -o /dev/null -w "%{http_code} %{content_type}\n" \
  "http://localhost:3000/src/data.json?import"
# -> 500 text/html   (app's SSR catch-all output)

End-to-end: import data from "./data.json" in a lazily-loaded route module, open the app from another device (or via hostname.local), navigate client-side to that route → dynamic import fails, blank page. Reload "fixes" it (SSR path doesn't need the client module).

Suggested fix

Add json to ASSET_EXT_RE (Vite transforms imported JSON into JS modules, so these requests belong to Vite). Possibly also worth including other Vite-transformable data extensions handled via query suffixes (?raw, ?url), but json is the one with a first-class Vite module transform.

Workaround

A user plugin registered before nitro() can flag JSON module requests with the existing escape hatch so the Nitro dev middleware leaves them to Vite:

function nitroJsonModuleFix(): Plugin {
  return {
    name: "nitro-json-module-fix",
    apply: "serve",
    configureServer(server) {
      server.middlewares.use((req: any, _res, next) => {
        if (/\.json\?(?:.*&)?(?:import|raw)\b/.test(req.url ?? "")) {
          req._nitroHandled = true
        }
        next()
      })
    }
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions