Skip to content

Commit b54f510

Browse files
authored
fix: Support async components (#686)
Closes #627.
1 parent 055e4e0 commit b54f510

File tree

4 files changed

+50
-1
lines changed

4 files changed

+50
-1
lines changed

src/layout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export default async function* layout(
9494
// stateless, and not relying on any React APIs such as hooks or suspense.
9595
// So we can safely evaluate it to render. Otherwise, an error will be
9696
// thrown by React.
97-
iter = layout((element.type as Function)(element.props), context)
97+
iter = layout(await (element.type as Function)(element.props), context)
9898
yield (await iter.next()).value as { word: string; locale?: string }[]
9999
}
100100

2.2 KB
Loading
2.63 KB
Loading

test/basic.test.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,55 @@ describe('Basic', () => {
9898
expect(toImage(svg, 100)).toMatchImageSnapshot()
9999
})
100100

101+
it('should support custom components', async () => {
102+
function MyComponent() {
103+
return <h1 style={{ fontSize: 16 }}>Hello from My Component</h1>
104+
}
105+
106+
const svg = await satori(
107+
<div
108+
style={{
109+
backgroundColor: '#ff0',
110+
width: '100%',
111+
height: '100%',
112+
display: 'flex',
113+
}}
114+
>
115+
<MyComponent />
116+
</div>,
117+
{
118+
width: 100,
119+
height: 100,
120+
fonts,
121+
}
122+
)
123+
expect(toImage(svg, 100)).toMatchImageSnapshot()
124+
125+
async function MyAsyncComponent() {
126+
await new Promise((resolve) => setTimeout(resolve, 0))
127+
return <h1 style={{ fontSize: 16 }}>Hello from My Async Component</h1>
128+
}
129+
const svg2 = await satori(
130+
<div
131+
style={{
132+
backgroundColor: '#ff0',
133+
width: '100%',
134+
height: '100%',
135+
display: 'flex',
136+
}}
137+
>
138+
{/* @ts-ignore */}
139+
<MyAsyncComponent />
140+
</div>,
141+
{
142+
width: 100,
143+
height: 100,
144+
fonts,
145+
}
146+
)
147+
expect(toImage(svg2, 100)).toMatchImageSnapshot()
148+
})
149+
101150
it('should combine textNodes correctly', async () => {
102151
const svg = await satori(
103152
<div

0 commit comments

Comments
 (0)