用于 React String SSR 渲染出字符串,配合 createRequestHandler
使用
import {
renderString,
createRequestHandler,
} from '@modern-js/runtime/ssr/server';
const handleRequest = async (request, ServerRoot, options) => {
const body = await renderString(request, <ServerRoot />, options);
return new Response(body, {
headers: {
'content-type': 'text/html; charset=utf-8',
},
});
};
export default createRequestHandler(handleRequest);
export type RenderString = (
request: Request,
serverRoot: React.ReactElement,
optinos: RenderOptions,
) => Promise<string>;
import {
renderString,
createRequestHandler,
} from '@modern-js/runtime/ssr/server';
const handleRequest = async (request, ServerRoot, options) => {
// do something before render
const body = await renderString(request, <ServerRoot />, options);
const newBody = body + '<div>Byte-Dance</div>';
return new Response(newBody, {
headers: {
'content-type': 'text/html; charset=utf-8',
},
});
};
export default createRequestHandler(handleRequest);