Skip to content

[v2] Regression: Response sended before first stream payload #1510

Description

@katywings

Environment

  • h3: 2.0.1-rc.25
  • node: 24.18.0

Reproduction

v2.0.1-rc.25 (Broken status/headers)

import { H3, serve } from "h3";

const app = new H3().get("/", async (event) => {
  return ReadableStream.from((async function* () {
    event.res.status = 201;
    event.res.headers.set("hello", "world");

    yield "Hello World";
  })())
});

serve(app, { port: 3000 });

v1.15.11 (Working status/headers)

import { setResponseHeader, setResponseStatus } from "h3";
import { createApp, defineEventHandler, sendStream, toNodeListener } from "h3";
import { createServer } from "http";

const app = createApp();
app.use("/", defineEventHandler(async (event) => {
  return ReadableStream.from((async function* () {
    setResponseStatus(event, 201);
    setResponseHeader(event, "hello", "world");

    yield "Hello World";
  })())
}));

createServer(toNodeListener(app)).listen(3000)

Describe the bug

h3 v1 waited with sending the response until the first async stream payload. This allowed setting response status codes and headers from within a stream (as long as it was part of the first payload). In h3 v2 you must set response status codes and headers before returning the stream. Response changes made while async rendering the first stream payload are now ignored. This makes it impossible to set response status/headers depending on async data within stream based frameworks such as SolidStart.

Additional context

Related: solidjs/solid-start#2229

Logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions