RenderContext is responsible for resource management and HTML generation during Server-Side Rendering, providing module dependency collection and resource injection order constraints.
Get an instance through esmx.render():
async server(esmx) {
const server = http.createServer((req, res) => {
esmx.middleware(req, res, async () => {
const rc = await esmx.render({
params: {
url: req.url
}
});
res.end(rc.html);
});
});
}RenderContext collects module and resource dependencies during component rendering, avoiding preloading all resources.
importMetaSetcommit() methodRenderContext provides multiple methods to inject different types of resources, each carefully designed to optimize resource loading performance:
preload(): Preloads CSS and JS resources, supports priority configurationcss(): Injects first-screen stylesheets, supports critical CSS extractionimportmap(): Injects module import maps, supports dynamic path resolutionmoduleEntry(): Injects client entry module, supports multi-entry configurationmodulePreload(): Preloads module dependencies, supports on-demand loading strategyRenderContext strictly controls resource injection order. This order design is based on browser working principles and performance optimization considerations:
head section:
preload(): Preloads CSS and JS resources, letting the browser discover and start loading these resources as early as possiblecss(): Injects first-screen stylesheets, ensuring page styles are in place when content rendersbody section:
importmap(): Injects module import maps, defining ESM module path resolution rulesmoduleEntry(): Injects client entry module, must be executed after importmapmodulePreload(): Preloads module dependencies, must be executed after importmapA typical flow is as follows:
export default async (rc: RenderContext) => {
const app = createApp();
const html = await renderToString(app, {
importMetaSet: rc.importMetaSet
});
await rc.commit();
rc.html = `
<!DOCTYPE html>
<html>
<head>
${rc.preload()}
${rc.css()}
</head>
<body>
${html}
${rc.importmap()}
${rc.moduleEntry()}
${rc.modulePreload()}
</body>
</html>
`;
};RenderContext provides a flexible dynamic base path configuration mechanism, supporting dynamic setting of static resource base paths at runtime:
const rc = await esmx.render({
base: '/esmx',
params: {
url: req.url
}
});This mechanism is particularly suitable for the following scenarios:
Multi-Language Site Deployment
main-domain.com → Default language
main-domain.com/cn/ → Chinese site
main-domain.com/en/ → English siteMicro-Frontend Applications
RenderContext provides two Import Map modes:
Inline Mode (default)
JS Mode
Can choose appropriate mode through configuration:
const rc = await esmx.render({
importmapMode: 'js',
params: {
url: req.url
}
});RenderContext supports specifying the Server-Side Rendering entry function through the entryName configuration:
const rc = await esmx.render({
entryName: 'mobile',
params: {
url: req.url
}
});This mechanism is particularly suitable for the following scenarios:
// Mobile entry function
export const mobile = async (rc: RenderContext) => {};
export const desktop = async (rc: RenderContext) => {};A/B Testing
Special Rendering Requirements
Get RenderContext Instance
esmx.render() methodDependency Collection
importMetaSet.add(import.meta)commit() method immediately after rendering completesResource Injection
Performance Optimization