fix(react-renderer): fix overwriting headers when stream is enabled (#419)

* fix(react-renderer): fix overwriting headers when stream is enabled

* Create changeset

* Add test to set headers with stream enaabled
pull/423/head
yoshikouki 2024-03-17 10:40:58 +09:00 committed by GitHub
parent a5e59c4199
commit 6eaf2a14cd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 9 deletions

View File

@ -0,0 +1,5 @@
---
'@hono/react-renderer': patch
---
fix: overwriting headers when stream is enabled

View File

@ -30,15 +30,15 @@ const createRenderer =
React.createElement(RequestContext.Provider, { value: c }, node), React.createElement(RequestContext.Provider, { value: c }, node),
options.readableStreamOptions options.readableStreamOptions
) )
return c.body(stream, { if (options.stream === true) {
headers: c.header('Transfer-Encoding', 'chunked')
options.stream === true c.header('Content-Type', 'text/html; charset=UTF-8')
? { } else {
'Transfer-Encoding': 'chunked', for (const [key, value] of Object.entries(options.stream)) {
'Content-Type': 'text/html; charset=UTF-8', c.header(key, value)
} }
: options.stream, }
}) return c.body(stream)
} else { } else {
const docType = const docType =
typeof options?.docType === 'string' typeof options?.docType === 'string'

View File

@ -105,6 +105,23 @@ describe('Basic', () => {
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><body><h1>Hello</h1></body></html>' '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html><body><h1>Hello</h1></body></html>'
) )
}) })
it('Should return as streaming content with headers added in a handler', async () => {
const app = new Hono()
app.use(reactRenderer(({ children }) => <>{children}</>, { stream: true }))
app.get('/', (c) => {
c.header('X-Message-Set', 'Hello')
c.header('X-Message-Append', 'Hello', { append: true })
return c.render(<h1>Hi</h1>, { title: 'Hi' })
})
const res = await app.request('/')
expect(res.status).toBe(200)
expect(res.headers.get('Transfer-Encoding')).toBe('chunked')
expect(res.headers.get('Content-Type')).toBe('text/html; charset=UTF-8')
expect(res.headers.get('X-Message-Set')).toBe('Hello')
expect(res.headers.get('X-Message-Append')).toBe('Hello')
expect(await res.text()).toBe('<h1>Hi</h1>')
})
}) })
describe('Streaming', () => { describe('Streaming', () => {