Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[adapter-node] fetch provided by load() fails to resolve URL & emits irrelevant error #13168

Open
hugeblank opened this issue Dec 16, 2024 · 3 comments
Labels
documentation Improvements or additions to documentation pkg:adapter-node

Comments

@hugeblank
Copy link

Describe the bug

My project works perfectly fine using npm run dev, and npm run preview. However on running npm run build, I try to test the server using PORT=4200 node build/index.js. It appears to launch, but then on accessing the page, even via http (http://localhost:4200) it emits an error. The error:

TypeError: fetch failed
    at node:internal/deps/undici/undici:13392:13
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
    at async fetch (file:///run/media/watergate/programming/grayhaze.live/affront/build/server/index.js:4289:18) {
  [cause]: [Error: 00E290EB14730000:error:0A0000C6:SSL routines:tls_get_more_records:packet length too long:ssl/record/methods/tls_common.c:662:
  ] {
    library: 'SSL routines',
    reason: 'packet length too long',
    code: 'ERR_SSL_PACKET_LENGTH_TOO_LONG'
  }
}

Reproduction

I isolated it down to this line of my project which notably makes use of the fetch given by sveltekit. When I remove the definition on the line above I get a richer error about it being unable to parse the URL. When filling in the full domain and path of the resource svelte's fetch gives a bit richer of an error mentioning that CORS headers are missing. Despite that, when I remove svelte's fetch again and use the full domain name with node's fetch it works outright, no problems.

Logs

No response

System Info

System:
    OS: Linux 6.12 Arch Linux
    CPU: (16) x64 AMD Ryzen 7 5800X3D 8-Core Processor
    Memory: 20.96 GB / 31.26 GB
    Container: Yes
    Shell: 5.9 - /usr/bin/zsh
  Binaries:
    Node: 23.4.0 - /usr/bin/node
    Yarn: 1.22.22 - /usr/bin/yarn
    npm: 10.9.0 - /usr/bin/npm
    pnpm: 9.14.4 - /usr/bin/pnpm
    bun: 1.0.2 - /usr/bin/bun
  Browsers:
    Chromium: 131.0.6778.108
  npmPackages:
    @sveltejs/adapter-auto: ^3.0.0 => 3.3.1 
    @sveltejs/adapter-node: ^5.2.9 => 5.2.10 
    @sveltejs/kit: ^2.11.0 => 2.11.1 
    @sveltejs/vite-plugin-svelte: ^5.0.0 => 5.0.2 
    svelte: ^5.0.0 => 5.14.0 
    vite: ^6.0.0 => 6.0.3

Severity

serious, but I can work around it

Additional Information

No response

@eltigerchino
Copy link
Member

eltigerchino commented Dec 17, 2024

Please provide a minimal reproduction, not your project.

@hugeblank
Copy link
Author

hugeblank commented Dec 31, 2024

repro.zip

  1. npm i; npm run build; node build/index.js
  2. Navigate to sverdle, then refresh the page. The error is present on the server side.

All that was necessary besides the demo project and adapter-node:

export async function load({ fetch }) {
	console.log(await fetch("/robots.txt"))
}

@eltigerchino eltigerchino added pkg:adapter-node documentation Improvements or additions to documentation and removed awaiting submitter labels Dec 31, 2024
@eltigerchino
Copy link
Member

eltigerchino commented Dec 31, 2024

You'll need to provide the ORIGIN env variable when testing your build locally:

# or e.g. for local previewing and testing
ORIGIN=http://localhost:3000 node build

https://svelte.dev/docs/kit/adapter-node#Environment-variables-ORIGIN-PROTOCOL_HEADER-HOST_HEADER-and-PORT_HEADER

This solves the issue where SvelteKit's load function's fetch fails to correctly construct the absolute URL based on the relative URL provided to it. It fails because the node adapter has been purposely designed to assume it's sitting behind a HTTPS reverse-proxy so it constructs an absolute URL such as "https://localhost:3000/robots.txt` when fetching /robots.txt on the server. However, the https URL doesn't exist when running we're previewing it locally with node.

We could probably document this fact better.

This is the intended behavior. Production apps built with adapter-node assume they are behind an HTTPS proxy (because, in production, they generally are), but this can be configured with runtime env vars - kit.svelte.dev/docs/adapter-node#environment-variables-origin-protocol-header-and-host-header

#10078 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation pkg:adapter-node
Projects
None yet
Development

No branches or pull requests

2 participants