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
Provenance
- Commit
58825a7- Tree
b1c133c- Author
3w36zj6 <52315048+3w36zj6@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
0e80acb
2 files changed+65 added−3 removed
modified:src/middleware/jsx-renderer/index.test.tsx+56 added
405 unmodified lines406 expect(res.status).toBe(200)407 expect(await res.text()).toBe('<!DOCTYPE html><div>Hi</div>')408 })409 410+ it('Should accept function-based options', async () => {411+ type Env = { Bindings: { HONO_STREAMING?: boolean } }412+ const app = new Hono<Env>()413+414+ const Component = async () => {415+ return <div>Component</div>416+ }417+418+ 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+ )434+435+ 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+ })445+446+ 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...')453+454+ 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+ })465+466 describe('keep context status', async () => {467 it('Should keep context status', async () => {468 const app = new Hono()469 app.use(45 unmodified linesmodified:src/middleware/jsx-renderer/index.ts+9 added-3 removed
29 unmodified lines303132333434353637383940414243444566 unmodified lines11211311411511011611711211811912012112243 unmodified lines29 unmodified lines c: Context) => HtmlEscapedString | Promise<HtmlEscapedString>
const createRenderer = (c: Context, Layout: FC, component?: Component, options?: RendererOptions) => ( c: Context, Layout: FC, component?: Component, options?: RendererOptions | ((c: Context) => RendererOptions) ) => (children: JSXNode, props: PropsForRenderer) => { options = typeof options === 'function' ? options(c) : options const docType = typeof options?.docType === 'string' ? options.docType : options?.docType === false66 unmodified lines * return c.render(<h1>About me!</h1>) * }) * ``` */export const jsxRenderer = (export const jsxRenderer = <E extends Env = Env>( component?: ComponentWithChildren, options?: RendererOptions options?: RendererOptions | ((c: Context<E>) => RendererOptions)): MiddlewareHandler => function jsxRenderer(c, next) { const Layout = (c.getLayout() ?? Fragment) as FC if (component) {43 unmodified lines