Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Regression since 0e08003 causes ALL calls to globalThis.fetch to route directly to the Nitro router even if it's an external address #35321

Description

@juls0730

Environment

│ ╭── Nuxt project info (copied to clipboard) ─────────────╮
│ │                                                        │
│ │  Operating system      Linux 7.0.10-2-cachyos          │
│ │  CPU                   AMD Ryzen 5 5500 (12 cores)     │
│ │  Node.js version       v24.14.0                        │
│ │  nuxt/cli version      3.36.0-20260608-223204-f869c61  │
│ │  Package manager       npm 11.9.0                      │
│ │  Nuxt version          5.0.0-29675450.fd7056aa         │
│ │  Nitro version         3.0.260522-beta                 │
│ │  Builder               vite 8.0.16                     │
│ │  Config                compatibilityDate               │
│ │  Modules               -                               │
│ │                                                        │
│ ╰────────────────────────────────────────────────────────╯
│

Reproduction

  • Create a Nuxt v5 project
  • Create a server route that fetches an external URL, I will be using https://jsonplaceholder.typicode.com/todos/1 in server/routes/test-fetch.get.ts
    import { defineEventHandler } from "nitro/h3";
    
    export default defineEventHandler(async () => {
      let externalResult: any;
      let externalError: any;
    
      try {
        const response = await globalThis.fetch(
          "https://jsonplaceholder.typicode.com/todos/1",
          {
            redirect: "error",
            headers: { accept: "application/json" },
          },
        );
        externalResult = await response.json();
      } catch (err: any) {
        externalError = {
          message: err.message,
          name: err.name,
        };
      }
    
      return {
        externalSuccess: !!externalResult,
        externalTitle: externalResult?.title,
        externalError,
      };
    });
  • start the nuxt server
  • curl localhost:3000/test-fetch and you will see externalTitle is delectus aut autem (the correct response)
  • Trigger SSR by loading the index page curl localhost:3000
  • curl localhost:3000/test-fetch: error. Furthermore, if you have a route at /todos/1 the response will be from that file rather than from jsonplaceholder

Describe the bug

After triggering a SSR, all server fetches will use the globalThis.fetch implementation that gets clobbered here

export const dollarFetchTemplate: NuxtTemplate = {
filename: 'fetch.server.mjs',
getContents () {
return [
'import { $fetch } from \'ofetch\'',
'import { baseURL } from \'#internal/nuxt/paths\'',
'import { serverFetch } from "nitro";',
'globalThis.fetch = serverFetch',
'if (!globalThis.$fetch) {',
' globalThis.$fetch = $fetch.create({',
' baseURL: baseURL()',
' })',
'}',
].join('\n')
},
}
which causes every URL fetched on the server to be routed through Nitro regardless of if it's an external URL or not.

Additional context

No response

Logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    5.x❗ p4-importantImportant issue, should be addressed soon

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions