Skip to content
Myr

feat(jsx-renderer): support function-based options (#4780)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by 3w36zj6 · committed by GitHub · parent 0e80acb · Browse files · 2 files+65 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+65 added−3 removed

  • tests:+56 added−0 removed
  • other:+9 added−3 removed

2 files+65 added−3 removed

modified:src/middleware/jsx-renderer/index.test.tsx+56 added
405 unmodified lines
406 expect(res.status).toBe(200)407 expect(await res.text()).toBe('<!DOCTYPE html><div>Hi</div>')408 })409410 it('Should accept function-based options', async () => {411 type Env = { Bindings: { HONO_STREAMING?: boolean } }412 const app = new Hono<Env>()413414 const Component = async () => {415 return <div>Component</div>416 }417418 app.use(419 '*',420 jsxRenderer<Env>(421 ({ children }) => {422 return (423 <html>424 <body>{children}</body>425 </html>426 )427 },428 (c) => {429 expectTypeOf(c.env?.HONO_STREAMING).toEqualTypeOf<boolean | undefined>()430 return { docType: true, stream: c.env?.HONO_STREAMING ?? true }431 }432 )433 )434435 app.get('/', async (c) => {436 return c.render(437 <div>438 <Suspense fallback={'loading...'}>439 <Component />440 </Suspense>441 </div>,442 { title: 'Suspense test' }443 )444 })445446 const resStream = await app.request('/')447 expect(resStream.status).toBe(200)448 expect(resStream.headers.get('Transfer-Encoding')).toBe('chunked')449 const textStream = await resStream.text()450 expect(textStream).toContain('<template')451 expect(textStream).toContain('<script')452 expect(textStream).toContain('loading...')453454 const resNotStream = await app.request('/', undefined, { HONO_STREAMING: false })455 expect(resNotStream.status).toBe(200)456 expect(resNotStream.headers.get('Transfer-Encoding')).toBeNull()457 const textNotStream = await resNotStream.text()458 expect(textNotStream).not.toContain('<template')459 expect(textNotStream).not.toContain('<script')460 expect(textNotStream).not.toContain('loading...')461 expect(textNotStream).toBe(462 '<!DOCTYPE html><html><body><div><div>Component</div></div></body></html>'463 )464 })465466 describe('keep context status', async () => {467 it('Should keep context status', async () => {468 const app = new Hono()469 app.use(
45 unmodified lines
modified:src/middleware/jsx-renderer/index.ts+9 added-3 removed