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.
importMetaSet.commit() method.RenderContext 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 configuration.css(): Injects first-screen stylesheets, supports critical CSS extraction.importmap(): Injects module import maps, supports dynamic path resolution.moduleEntry(): Injects client entry module, supports multi-entry configuration.modulePreload(): Preloads module dependencies, supports on-demand loading strategy.RenderContext 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 possible.css(): Injects first-screen stylesheets, ensuring page styles are in place when content renders.body section:
importmap(): Injects module import maps, defining ESM module path resolution rules.moduleEntry(): Injects client entry module, must be executed after importmap.modulePreload(): Preloads module dependencies, must be executed after importmap.A typical flow is as follows:
import { createApp } from 'vue';
import { renderToString } from '@vue/server-renderer';
import type { RenderContext } from '@esmx/core';
import App from './app.vue';
export default async (rc: RenderContext) => {
const app = createApp(App);
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
commit() method immediately after rendering completes to submit the collected results.Resource Injection
body.importmap comes before moduleEntry.Performance Optimization
preload to preload critical resources.modulePreload to optimize module loading.