import fs from "fs";
import ReactDOMServer from "react-dom/server.js";
import { preambleCode } from "@vitejs/plugin-react-refresh";
// import App from "/src/App";
const APP_PLACEHOLDER = "";
const VITE_PLACEHOLDER = "";
export default (vite) => {
return async function ssr(req, res, next) {
console.log("ssr");
const template = fs.readFileSync(process.cwd() + "/template/index.html", {
encoding: "utf-8",
});
const [pageBeforeApp, pageAfterApp] = template.split(APP_PLACEHOLDER);
res.write(
pageBeforeApp.replace(
VITE_PLACEHOLDER,
`
`
)
);
const { render } = await vite.ssrLoadModule("/src/main-server");
// ToDo also return preload links
const stream = await render(req.originalUrl);
stream.pipe(res, { end: false });
stream.on("end", () => res.end(pageAfterApp));
// if string
// console.log("content", stream);
// res.write(stream);
// res.end(pageAfterApp);
};
};