-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathprerender.ts
31 lines (24 loc) · 992 Bytes
/
prerender.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* eslint-env node */
// ^^^^ This comment is need to prevent browser bundles of this file
import type { VNode } from 'preact'
import type { PrerenderResult, PrerenderOptions } from 'preact-iso/prerender'
import prerender from 'preact-iso/prerender'
import type { Configuration } from 'twind'
import type { TwindPreactConfiguration } from '@twind/preact'
import { asyncVirtualSheet, getStyleTag } from 'twind/server'
import { setup } from '@twind/preact'
export default function prerenderWithTwind(
config: Configuration & TwindPreactConfiguration,
render: (data: any) => VNode,
options?: PrerenderOptions,
): (data: any) => Promise<PrerenderResult> {
const sheet = asyncVirtualSheet()
setup({ ...config, sheet })
// Ensure to start a new async scope
return (data) =>
Promise.resolve().then(async () => {
await sheet.reset()
let { html, ...rest } = await prerender(render(data), options)
return { ...rest, html: getStyleTag(sheet) + html }
})
}