[英]SSR in React18 using renderToPipableStream - Server Generated HTML and Client Side HTML file UI are not matching
I was trying to make Server Side Rendering using Node,Express and React18 feature.我试图使用 Node、Express 和 React18 功能进行服务器端渲染。 The method I used was with renderToPipeableStream and not with renderToString.
我使用的方法是使用renderToPipeableStream而不是使用 renderToString。
Link for the CODE -代码链接 -
NOTE:笔记:
ISSUE:问题:
<div id="root"> <!-received chunk data should inject here-></div>
)<div id="root"> <!-received chunk data should inject here-></div>
) WHAT I TRIED:我试过什么:
Please provide me the solution to match the Server generated HTML and Client side HTML so that I can hydrate the UI.请为我提供匹配服务器生成的 HTML 和客户端 HTML 的解决方案,以便我可以滋润 UI。
The thing is, when you're working with react streaming, there's no real way to inject something in it.问题是,当您使用 React Streaming 时,没有真正的方法可以在其中注入一些东西。
The solution would be to provide your own Document component like in Next.js.解决方案是提供您自己的文档组件,如 Next.js。
const Document = ({ title, js, css, children }) => {
return (
<html>
<head>
<meta charSet="utf-8" />
<title>{title}</title>
{js.map((src, i) => (
<script key={i} src={src} defer />
))}
{css.map((src, i) => (
<link key={i} href={src} rel="stylesheet" />
))}
</head>
<body>
<div id="root">{children}</div>
</body>
</html>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.