You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
909 B
JavaScript
32 lines
909 B
JavaScript
import fs from "fs";
|
|
import ReactDOMServer from "react-dom/server.js";
|
|
// import App from "/src/App";
|
|
|
|
const APP_PLACEHOLDER = "<!-- __REACT_SSR_APP__ -->";
|
|
|
|
export default (App) => {
|
|
console.log(App);
|
|
return async function ssr(req, res, next) {
|
|
// ToDo
|
|
console.log("ssr");
|
|
|
|
const template = fs.readFileSync(process.cwd() + "/template/index.html", {
|
|
encoding: "utf-8",
|
|
});
|
|
console.log("template", template);
|
|
|
|
const [pageBeforeApp, pageAfterApp] = template.split(APP_PLACEHOLDER);
|
|
|
|
console.log("start", pageBeforeApp);
|
|
res.write(pageBeforeApp);
|
|
const stream = ReactDOMServer.renderToNodeStream((await App).code);
|
|
stream.pipe(res, { end: false });
|
|
stream.on("end", () => res.end(pageAfterApp));
|
|
// const stream = ReactDOMServer.renderToString(App);
|
|
// console.log("content", stream);
|
|
// res.write(stream);
|
|
|
|
// res.end(pageAfterApp);
|
|
};
|
|
};
|