Skip to content

Server side fetch during dev and production runs different #4500

Closed

Description

@lutejka

Environment

nodejs

Reproduction

https://stackblitz.com/edit/github-yjfey7sb?file=server%2Froutes%2Findex.ts

Describe the bug

During development the the server side fetch can not resolve files in the public directory.

Once the app is build the server side fetch can resolve the public files and everything works as expected

Additional context

This was found in nuxt: nuxt/nuxt#13857

Logs

Activity

  1. added
    pending triage
    on Jul 30, 2026
  2. meta-syntax commented on Aug 12, 2026

    @meta-syntax

    I can confirm the reported behavior. Reproduced on both v2 (nitropack 2.13.4) and v3 (main branch, commit 16ff280, built locally), running on real Node.js v22.20.0.

    curl localhost:3000/ (handler does internal fetch) curl localhost:3000/test.json
    dev ❌ 404 ✅ 200
    build + node .output/server/index.mjs ✅ 200 ✅ 200

    Results are identical on v2 and v3.

    Repro setup

    Both versions: server/public/test.json with { "hello": "world" }.

    v2 (nitropack 2.13.4)

    // nitro.config.ts
    import { defineNitroConfig } from "nitropack/config"
    
    export default defineNitroConfig({ srcDir: "server" })
    // server/routes/index.ts
    export default defineEventHandler(async () => {
      try {
        const data = await $fetch("/test.json")
        return { ok: true, data }
      } catch (err) {
        return { ok: false, error: String(err) }
      }
    })

    v3 (main)

    // nitro.config.ts
    import { defineConfig } from "nitro"
    export default defineConfig({ serverDir: "./server" })
    // server/routes/index.ts
    import { defineHandler } from "h3"
    import { fetch } from "nitro/app"
    
    export default defineHandler(async () => {
      const res = await fetch("/test.json")
      return { ok: res.ok, status: res.status, data: res.ok ? await res.json() : null }
    })

    If I'm reading the source correctly, this is consistent with how the internal fetch is wired: requests starting with / are dispatched directly to the h3 app, without going through HTTP (src/runtime/internal/app.ts).

    The static assets handler is mounted in dev too, but it looks assets up in a manifest generated by scanning output.publicDir (src/build/virtual/public-assets.ts), and that directory is only populated by copyPublicAssets() during nitro build.

    In dev the manifest stays empty, so the handler falls through and the router returns 404.
    Public assets in dev are actually served by the dev server process (src/dev/app.ts) — outside the worker's h3 app, so a direct app-call fetch never reaches that layer.

    So the issue can be narrowed down to: internal (server-side) fetch of a public asset while the dev server is running. Production (node-server preset) works, and fetching /test.json directly over HTTP works in both modes.

    If this is confirmed as a bug (and not intended dev behavior), I'd be happy to work on a fix — any pointers on the preferred direction would be appreciated.

  3. added
    bug
    and removed
    pending triage
    on Sep 2, 2026
  4. added theissue type on Nov 10, 2026
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

    bug

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions