middleware icon indicating copy to clipboard operation
middleware copied to clipboard

[react-renderer] After enabling Stream option, HTTP header is overwritten

Open yoshikouki opened this issue 1 year ago • 3 comments

It seems the cause may be located here: https://github.com/honojs/middleware/blob/74dfa07dedb9b172c7434d4f564199d73aa75d94/packages/react-renderer/src/react-renderer.ts#L35-L40

Just wanted to share this for now.

Reproduction code

import { reactRenderer } from "@hono/react-renderer";
import { Hono } from "hono";
import { setCookie } from "hono/cookie";

const app = new Hono();

app.get(
  "*",
  reactRenderer(({ children }) => {
    return (
      <html>
        <body>
          <h1>React + Hono</h1>
          <div>{children}</div>
        </body>
      </html>
    );
  }, {
    stream: true,
  })
);

app.get("/", (c) => {
  setCookie(c, "id", crypto.randomUUID());

  return c.render(<p>Welcome!</p>);
});

export default {
  port: 3333,
  fetch: app.fetch,
};

stream: true

❯ curl -I localhost:3333
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Date: Tue, 12 Mar 2024 15:22:46 GMT
Content-Length: 0

stream: false

❯ curl -I localhost:3333
HTTP/1.1 200 OK
set-cookie: id=535071ee-f54e-4ddd-9226-ba5c20bb28b8; Path=/
Content-Type: text/html; charset=UTF-8
Date: Tue, 12 Mar 2024 15:22:59 GMT
Content-Length: 0

yoshikouki avatar Mar 12 '24 15:03 yoshikouki

This worked! I'll make PR

      if (options.stream === true) {
        c.header("Transfer-Encoding", "chunked");
        c.header("Content-Type", "text/html; charset=UTF-8");
      } else {
        for (const [key, value] of Object.entries(options.stream)) {
          c.header(key, value);
        }
      }
      return c.body(stream);

stream: true

❯ curl -I localhost:3333
HTTP/1.1 200 OK
set-cookie: id=57681003-5d3c-451f-9637-c033eb6bbc58; Path=/
Content-Type: text/html; charset=UTF-8
Date: Wed, 13 Mar 2024 14:03:29 GMT
Content-Length: 0

yoshikouki avatar Mar 13 '24 13:03 yoshikouki

Hi @yoshikouki

I'll make PR

Please! Then, I'll review it!

yusukebe avatar Mar 13 '24 14:03 yusukebe

@yusukebe Please review🙏

  • https://github.com/honojs/middleware/pull/419

yoshikouki avatar Mar 13 '24 14:03 yoshikouki