跳到主要内容

React DOM legacy server impl

一、作用

二、渲染为字符串实现

备注
function renderToStringImpl(
children: ReactNodeList,
options: void | ServerOptions,
generateStaticMarkup: boolean,
abortReason: string,
): string {
let didFatal = false;
let fatalError = null;
let result = '';
const destination = {
push(chunk) {
if (chunk !== null) {
result += chunk;
}
return true;
},
destroy(error) {
didFatal = true;
fatalError = error;
},
};

let readyToStream = false;
function onShellReady() {
readyToStream = true;
}
const resumableState = createResumableState(
options ? options.identifierPrefix : undefined,
undefined,
);
const request = createRequest(
children,
resumableState,
createRenderState(resumableState, generateStaticMarkup),
createRootFormatContext(),
Infinity,
onError,
undefined,
onShellReady,
undefined,
undefined,
undefined,
);
startWork(request);
// If anything suspended and is still pending, we'll abort it before writing.
// 如果有任何挂起的内容仍在等待,我们将在写入之前中止它。
// That way we write only client-rendered boundaries from the start.
// 这样我们从一开始就只写入客户端渲染的边界。
abort(request, abortReason);
startFlowing(request, destination);
if (didFatal && fatalError !== abortReason) {
throw fatalError;
}

if (!readyToStream) {
// Note: This error message is the one we use on the client. It doesn't
// really make sense here. But this is the legacy server renderer, anyway.
// 注意:此错误信息是我们在客户端使用的。在这里它并不真正有意义。但无论如何,这是传统的服务器渲染器。
// We're going to delete it soon.
// 我们很快会删除它。
throw new Error(
'A component suspended while responding to synchronous input. This ' +
'will cause the UI to be replaced with a loading indicator. To fix, ' +
'updates that suspend should be wrapped with startTransition.',
);
}

return result;
}

三、工具

1. 出错时

function onError() {
// Non-fatal errors are ignored.
// 非致命错误会被忽略。
}

四、类型

1. 服务器选项

type ServerOptions = {
identifierPrefix?: string;
};

五、转导