Skip to content

chore: derive content-length from fixed response bodies in setResponse - #16794

Merged
Rich-Harris merged 7 commits into
version-3from
response-json-runtime
Aug 14, 2026
Merged

chore: derive content-length from fixed response bodies in setResponse#16794
Rich-Harris merged 7 commits into
version-3from
response-json-runtime

Conversation

@Nic-Polumeyv

@Nic-Polumeyv Nic-Polumeyv commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

On Node, setResponse streams every response.body through a reader loop, so a response without a pre-computed content-length header is served with chunked transfer encoding. That is why the runtime can't move off the deprecated json/text helpers, whose only load-bearing job is stamping that header, and why a plain Response.json returned from an endpoint is served chunked today. Every other runtime derives the header from fixed bodies at serve time.

setResponse now races the body's first reads against a setImmediate deadline before writing the head. A fixed body settles value-then-done on microtasks, so it is sent with a measured content-length; a body that leaves a read pending is a genuine stream and goes through the existing streaming path unchanged, with its headers delayed by at most one tick. Nothing is added when the response already carries a content-length or a transfer-encoding (proxied responses can carry the latter, and the pair would be invalid). Same approach as hono's node-server.

json/text are untouched. Retiring them internally becomes a follow-up; its one catch is the http.response.body.size span attribute, which reads the content-length header.

#16804 is stacked on this.

@pkg-svelte-dev

pkg-svelte-dev Bot commented Aug 13, 2026

Copy link
Copy Markdown

Install the latest version of @sveltejs/kit from 44c0e5f:

pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/44c0e5f63073bf257f37c9c4b540be6bff19ab66

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/kit/pr/16794

@changeset-bot

changeset-bot Bot commented Aug 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 44c0e5f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@svelte-docs-bot

Copy link
Copy Markdown

@WolfieLeader

Copy link
Copy Markdown
Contributor

Noice, do you think it should be updated in other parts as well? also would the json from sveltekit would be deprecated from the develop side as well? if so what about using the json as an alias to Response.json?

@teemingc

teemingc commented Aug 14, 2026

Copy link
Copy Markdown
Member

I think for that exact reason you've mentioned (no content-length headers) we've avoided switching away from it for now.

@Nic-Polumeyv
Nic-Polumeyv force-pushed the response-json-runtime branch from 1a5b9ac to 6ce4a9f Compare August 14, 2026 16:04
@Nic-Polumeyv Nic-Polumeyv changed the title chore: use Response.json in runtime responses chore: compute content-length in setResponse instead of the json and text helpers Aug 14, 2026
@Nic-Polumeyv
Nic-Polumeyv changed the base branch from live-query-teardown to version-3 August 14, 2026 16:04
Comment thread packages/kit/src/exports/index.js Outdated
@ottomated

Copy link
Copy Markdown
Contributor

It seems a bit complicated for something that no longer gets rid of the need for json() and instead is only really an optimization (have you benchmarked it)?

@Nic-Polumeyv
Nic-Polumeyv marked this pull request as draft August 14, 2026 17:02
@Nic-Polumeyv

Copy link
Copy Markdown
Contributor Author

You're right. I think there is a better redesign here. wip

@Nic-Polumeyv Nic-Polumeyv changed the title chore: compute content-length in setResponse instead of the json and text helpers chore: derive content-length from fixed response bodies in setResponse Aug 14, 2026
@WolfieLeader

Copy link
Copy Markdown
Contributor

Lol didn't mean to make you doing a redesign😅

@Nic-Polumeyv

Copy link
Copy Markdown
Contributor Author

Yeah, I wasn't trying to do one either, unfortunately node has to always be in the way. A redesign was necessary to make this PR appealing.

@Nic-Polumeyv

Copy link
Copy Markdown
Contributor Author

@ottomated I benchmarked this rework with autocannon, setResponse in isolation.

small JSON ~30k req/s on both, 20KB bodies ~300 vs ~430 MB/s, 5MB bodies ~1GB/s on both. So yes, more performant: ~40% more throughput on page-sized responses, parity everywhere else!

@Rich-Harris Rich-Harris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a shame that this adds some complexity but i don't see a simpler alternative

@Rich-Harris
Rich-Harris merged commit 97d0fab into version-3 Aug 14, 2026
27 checks passed
@Rich-Harris
Rich-Harris deleted the response-json-runtime branch August 14, 2026 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants