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.
42 lines
1.1 KiB
JavaScript
42 lines
1.1 KiB
JavaScript
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 = "<!-- __REACT_SSR_APP__ -->";
|
|
const VITE_PLACEHOLDER = "<!-- __VITE_SSR_BOOTSTRAP -->";
|
|
|
|
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,
|
|
`<script type="module" src="/@vite/client"></script>
|
|
<script type="module">
|
|
${preambleCode}
|
|
</script>`
|
|
)
|
|
);
|
|
|
|
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);
|
|
};
|
|
};
|