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 enaabledpull/423/head
parent
a5e59c4199
commit
6eaf2a14cd
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'@hono/react-renderer': patch
|
||||
---
|
||||
|
||||
fix: overwriting headers when stream is enabled
|
|
@ -30,15 +30,15 @@ const createRenderer =
|
|||
React.createElement(RequestContext.Provider, { value: c }, node),
|
||||
options.readableStreamOptions
|
||||
)
|
||||
return c.body(stream, {
|
||||
headers:
|
||||
options.stream === true
|
||||
? {
|
||||
'Transfer-Encoding': 'chunked',
|
||||
'Content-Type': 'text/html; charset=UTF-8',
|
||||
}
|
||||
: options.stream,
|
||||
})
|
||||
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)
|
||||
} else {
|
||||
const docType =
|
||||
typeof options?.docType === 'string'
|
||||
|
|
|
@ -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>'
|
||||
)
|
||||
})
|
||||
|
||||
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', () => {
|
||||
|
|
Loading…
Reference in New Issue