Skip to content
Myr

fix(jsx): preserve context when using await before html helper (#4662)

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

* fix(jsx): preserve context when using await before html helper When an async component used await before returning html with children, the context was being popped synchronously in the finally block before the Promise resolved. This caused children to lose access to the context. The fix delays the pop() until the Promise resolves (or rejects), ensuring context remains available throughout the async component's execution. Fixes #4582 * refactor: use finally() for context cleanup per review * chore: remove package-lock.json ---------

authored by Kai Gritun · committed by GitHub · parent f7d272a · Browse files · 2 files+44 added−2 removed
This commit

State

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

Trailers

  • Co-authored-by: Kai Gritun <kai@kaigritun.com>
  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

2 files+44 added−2 removed

  • tests:+38 added−0 removed
  • other:+6 added−2 removed

2 files+44 added−2 removed

modified:src/jsx/context.ts+6 added-2 removed
modified:src/jsx/index.test.tsx+38 added
1020 unmodified lines
1021 const nextRequest = <Consumer />1022 expect(nextRequest.toString()).toBe('<span>light</span>')1023 })1024 })10251026 describe('async with html helper', () => {1027 it('should preserve context when using await before html helper', async () => {1028 // Regression test for https://github.com/honojs/hono/issues/45821029 // Context was being popped before async children resolved1030 const AsyncParentWithHtml = async (props: { children?: any }) => {1031 await new Promise((r) => setTimeout(r, 10))1032 return html`<div>${props.children}</div>`1033 }10341035 const template = (1036 <ThemeContext.Provider value='dark'>1037 <AsyncParentWithHtml>1038 <Consumer />1039 </AsyncParentWithHtml>1040 </ThemeContext.Provider>1041 )1042 expect((await template.toString()).toString()).toBe('<div><span>dark</span></div>')1043 })10441045 it('should preserve nested context when using await before html helper', async () => {1046 const AsyncParentWithHtml = async (props: { children?: any }) => {1047 await new Promise((r) => setTimeout(r, 10))1048 return html`<div>${props.children}</div>`1049 }10501051 const template = (1052 <ThemeContext.Provider value='dark'>1053 <AsyncParentWithHtml>1054 <ThemeContext.Provider value='black'>1055 <Consumer />1056 </ThemeContext.Provider>1057 </AsyncParentWithHtml>1058 </ThemeContext.Provider>1059 )1060 expect((await template.toString()).toString()).toBe('<div><span>black</span></div>')1061 })1062 })1063})10641065describe('version', () => {1066 it('should be defined with semantic versioning format', () => {
44 unmodified lines