(node: React.ReactNode, options?: Options)
| 7 | import { readStream } from './read-stream'; |
| 8 | |
| 9 | export const render = async (node: React.ReactNode, options?: Options) => { |
| 10 | const reactDOMServer = await import('react-dom/server').then((m) => { |
| 11 | if ('default' in m) { |
| 12 | return m.default; |
| 13 | } |
| 14 | return m; |
| 15 | }); |
| 16 | |
| 17 | let html!: string; |
| 18 | await new Promise<void>((resolve, reject) => { |
| 19 | if ( |
| 20 | Object.hasOwn(reactDOMServer, 'renderToReadableStream') && |
| 21 | typeof WritableStream !== 'undefined' |
| 22 | ) { |
| 23 | const ErrorBoundary = createErrorBoundary(reject); |
| 24 | reactDOMServer |
| 25 | .renderToReadableStream( |
| 26 | <ErrorBoundary> |
| 27 | <Suspense>{node}</Suspense> |
| 28 | </ErrorBoundary>, |
| 29 | { |
| 30 | progressiveChunkSize: Number.POSITIVE_INFINITY, |
| 31 | onError(error) { |
| 32 | // Throw immediately when an error occurs to prevent CSR fallback |
| 33 | reject(error); |
| 34 | }, |
| 35 | }, |
| 36 | ) |
| 37 | .then(async (stream) => { |
| 38 | await stream.allReady; |
| 39 | return readStream(stream); |
| 40 | }) |
| 41 | .then((result) => { |
| 42 | html = result; |
| 43 | resolve(); |
| 44 | }) |
| 45 | .catch(reject); |
| 46 | } else { |
| 47 | const ErrorBoundary = createErrorBoundary(reject); |
| 48 | const stream = reactDOMServer.renderToPipeableStream( |
| 49 | <ErrorBoundary> |
| 50 | <Suspense>{node}</Suspense> |
| 51 | </ErrorBoundary>, |
| 52 | { |
| 53 | async onAllReady() { |
| 54 | html = await readStream(stream).then((s: string) => { |
| 55 | // Workaround for https://github.com/facebook/react/pull/26228 |
| 56 | // (fixed in React 19, not backported to 18) |
| 57 | return s.replaceAll('\0', ''); |
| 58 | }); |
| 59 | resolve(); |
| 60 | }, |
| 61 | onError(error) { |
| 62 | reject(error); |
| 63 | }, |
| 64 | progressiveChunkSize: Number.POSITIVE_INFINITY, |
| 65 | }, |
| 66 | ); |
no test coverage detected
searching dependent graphs…