When the project initiates server.ssr, Modern.js generates a default Server-Side entry. The sample code is as follows:
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);Users need to customize the Server-Side Rendering entry points, they can customize the server entry in src/entry.server.ts or src/{entryName}/entry.server.ts.
import {
renderString,
createRequestHandler,
} from '@modern-js/runtime/ssr/server';
import type { HandleRequest } from '@modern-js/runtime/ssr/server';
const handleRequest: HandleRequest = async (request, ServerRoot, options) => {
// do something before rendering
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',
'x-custom-header': 'abc',
},
});
};
export default createRequestHandler(handleRequest);