Client-Side Rendering (CSR) executes page rendering in the browser. When a Node.js service cannot be deployed, static index.html can be generated during the build phase to achieve pure Client-Side Rendering.
The following scenarios are recommended for using Client-Side Rendering:
The template should include resource injection and entry order: preload and css in head, while importmap, moduleEntry, and modulePreload should be in body.
import type { RenderContext } from '@esmx/core';
export default async (rc: RenderContext) => {
await rc.commit();
rc.html = `
<!DOCTYPE html>
<html>
<head>
${rc.preload()}
<title>Esmx</title>
${rc.css()}
</head>
<body>
<div id="app"></div>
${rc.importmap()}
${rc.moduleEntry()}
${rc.modulePreload()}
</body>
</html>
`;
};Static HTML files can be generated during the build phase through the postBuild hook:
import type { EsmxOptions } from '@esmx/core';
export default {
async postBuild(esmx) {
const rc = await esmx.render();
esmx.writeSync(
esmx.resolvePath('dist/client', 'index.html'),
rc.html
);
}
} satisfies EsmxOptions;