| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import type {ReactNodeList} from 'shared/ReactTypes'; |
| 11 | import type {ErrorInfo} from 'react-server/src/ReactFizzServer'; |
| 12 | |
| 13 | import ReactVersion from 'shared/ReactVersion'; |
| 14 | |
| 15 | import { |
| 16 | createRequest as createFizzRequest, |
| 17 | startWork as startFizzWork, |
| 18 | startFlowing as startFizzFlowing, |
| 19 | attachAbortSignal as attachFizzAbortSignal, |
| 20 | } from 'react-server/src/ReactFizzServer'; |
| 21 | |
| 22 | import { |
| 23 | createResumableState, |
| 24 | createRenderState, |
| 25 | createRootFormatContext, |
| 26 | } from './ReactFizzConfigMarkup'; |
| 27 | |
| 28 | type MarkupOptions = { |
| 29 | identifierPrefix?: string, |
| 30 | signal?: AbortSignal, |
| 31 | onError?: (error: mixed, errorInfo: ErrorInfo) => ?string, |
| 32 | }; |
| 33 | |
| 34 | export function experimental_renderToHTML( |
| 35 | children: ReactNodeList, |
| 36 | options?: MarkupOptions, |
| 37 | ): Promise<string> { |
| 38 | return new Promise((resolve, reject) => { |
| 39 | let buffer = ''; |
| 40 | const fizzDestination = { |
| 41 | push(chunk: string | null): boolean { |
| 42 | if (chunk !== null) { |
| 43 | buffer += chunk; |
| 44 | } else { |
| 45 | // null indicates that we finished |
| 46 | resolve(buffer); |
| 47 | } |
| 48 | return true; |
| 49 | }, |
| 50 | destroy(error: mixed) { |
| 51 | reject(error); |
| 52 | }, |
| 53 | }; |
| 54 | function handleError(error: mixed, errorInfo: ErrorInfo) { |
| 55 | // Any error rejects the promise, regardless of where it happened. |
| 56 | // Unlike other React SSR we don't want to put Suspense boundaries into |
| 57 | // client rendering mode because there's no client rendering here. |
| 58 | reject(error); |
| 59 | |
| 60 | const onError = options && options.onError; |
| 61 | if (onError) { |
| 62 | onError(error, errorInfo); |
| 63 | } |
| 64 | } |
| 65 | const resumableState = createResumableState( |
| 66 | options ? options.identifierPrefix : undefined, |
| 67 | undefined, |
| 68 | ); |
| 69 | const fizzRequest = createFizzRequest( |
| 70 | children, |
| 71 | resumableState, |
| 72 | createRenderState( |
| 73 | resumableState, |
| 74 | undefined, |
| 75 | undefined, |
| 76 | undefined, |
| 77 | undefined, |
| 78 | undefined, |
| 79 | ), |
| 80 | createRootFormatContext(), |
| 81 | Infinity, |
| 82 | handleError, |
| 83 | undefined, |
| 84 | undefined, |
| 85 | undefined, |
| 86 | undefined, |
| 87 | undefined, |
| 88 | undefined, |
| 89 | ); |
| 90 | if (options && options.signal) { |
| 91 | attachFizzAbortSignal(fizzRequest, options.signal); |
| 92 | } |
| 93 | startFizzWork(fizzRequest); |
| 94 | startFizzFlowing(fizzRequest, fizzDestination); |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | export {ReactVersion as version}; |