kit icon indicating copy to clipboard operation
kit copied to clipboard

Svelte kit's provided fetch is stripping body when "mode: no-cors" is provided and the fetch happens from a +server.js module

Open raythurnvoid opened this issue 2 years ago • 0 comments

Describe the bug

https://github.com/sveltejs/kit/discussions/8097

I'm using svelte-kit's provided fetch to call a MS api that respond with an error when the Origin Header is provided.

I've noticed that the Origin Header is not provided when mode: "no-cors" is provided but this will then cause the body to be stripped away from the response.

I'm using the Node's global fetch method to workaround the issue at the moment.

Reproduction

https://github.com/raythurnevoid/svelte-kit-no-cors-strip-body-bug-report

npm i
npm run dev -- --open
  1. An error like this will show up in console
Unexpected end of JSON input
SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at packageData (C:\workspace\rt0\svelte-layout-server-error\node_modules\undici\lib\fetch\body.js:553:19)
    at specConsumeBody (C:\workspace\rt0\svelte-layout-server-error\node_modules\undici\lib\fetch\body.js:517:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async GET (C:/workspace/rt0/svelte-layout-server-error/src/routes/test.json/+server.ts:5:18)
    at async render_endpoint (file:///C:/workspace/rt0/svelte-layout-server-error/node_modules/@sveltejs/kit/src/runtime/server/endpoint.js:45:20)
    at async resolve (file:///C:/workspace/rt0/svelte-layout-server-error/node_modules/@sveltejs/kit/src/runtime/server/index.js:401:17)
    at async respond (file:///C:/workspace/rt0/svelte-layout-server-error/node_modules/@sveltejs/kit/src/runtime/server/index.js:264:20)
    at async fetch (file:///C:/workspace/rt0/svelte-layout-server-error/node_modules/@sveltejs/kit/src/runtime/server/fetch.js:142:16)
    at async Object.fetch (file:///C:/workspace/rt0/svelte-layout-server-error/node_modules/@sveltejs/kit/src/runtime/server/fetch.js:25:10)
  1. In src/routes/+server.ts, replace event.fetch with fetch to make it work

Logs

No response

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (16) x64 AMD Ryzen 9 4900H with Radeon Graphics
    Memory: 22.95 GB / 31.42 GB
  Binaries:
    Node: 18.10.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.19.2 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.963.0), Chromium (108.0.1462.46)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    @sveltejs/adapter-node: 1.0.0-next.102 => 1.0.0-next.102 
    @sveltejs/kit: 1.0.0-next.571 => 1.0.0-next.571 
    svelte: ^3.53.1 => 3.53.1

Severity

annoyance

Additional Information

I guess that the correct behavior should be to never provide the Origin Header when the call is performed from a server-only module to match the behavior of the Node's fetch API.

I've taken a look at the undici fetch docs, and actually looks like they don't even read the mode argument.

raythurnvoid avatar Dec 14 '22 15:12 raythurnvoid