[RFC] Support for server-side rendering #2221
Closed
9aoy
started this conversation in
RFC Discussions
Replies: 1 comment
-
|
resolved by #2528 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
We plan to provide some APIs in rsbuild to make it easier for users to use server-side rendering.
Background
Currently, users can implement the ssr capability in rsbuild by themselves, but according to the practice of rsbuild-ssr-example, there are some problems:
output.manifestconfig #989Configuring SSR
Rsbuild will provide some SSR related APIs for users to use.
rsbuildServer.ssrLoadModule. Used to load and execute ssr module.rsbuildServer.getTransformedHtml. used to get the compiled HTML template.Type
Example
Therefore, users can use ssr in rsbuild like this:
import { createRsbuild } from '@rsbuild/core'; import express from 'express'; export const serverRender = (rsbuild, entryName) => async (req, res, next) => { + const { render } = await rsbuild.ssrLoadModule(entryName); + + const markup = render(); + + const template = await rsbuild.getTransformedHtml(entryName); + + const html = template.replace(`<!--app-content-->`, markup) + + res.status(200).set({ 'Content-Type': 'text/html' }).send(html) }; export async function startDevServer() { const rsbuild = await createRsbuild({}); const app = express(); const rsbuildServer = await rsbuild.createDevServer(); + app.get('/', serverRender(rsbuild, 'index')) app.use(rsbuildServer.middlewares); .... }and the user's server-entry and template might look like this:
template.html:
index.server.tsx:
Beta Was this translation helpful? Give feedback.
All reactions