mirror of
https://github.com/hanzoai/vite.git
synced 2026-10-30 19:36:16 +00:00
+3








bcd936335c
Co-authored-by: Hiroshi Ogawa <[email protected]> Co-authored-by: Dario Piotrowicz <[email protected]> Co-authored-by: Vladimir Sheremet <[email protected]> Co-authored-by: Arnaud Barré <[email protected]> Co-authored-by: Anthony Fu <[email protected]> Co-authored-by: Dominik G <[email protected]> Co-authored-by: Igor Minar <[email protected]> Co-authored-by: Viktor Lázár <[email protected]> Co-authored-by: Joaquín Sánchez <[email protected]> Co-authored-by: sapphi-red <[email protected]> Co-authored-by: bluwy <[email protected]>
25 lines
749 B
TypeScript
25 lines
749 B
TypeScript
import ReactDomServer from 'react-dom/server'
|
|
import type { Connect, ViteDevServer } from 'vite'
|
|
import Root from './root'
|
|
|
|
const handler: Connect.NextHandleFunction = async (_req, res) => {
|
|
const ssrHtml = ReactDomServer.renderToString(<Root />)
|
|
let html = await importHtml()
|
|
html = html.replace(/<body>/, `<body><div id="root">${ssrHtml}</div>`)
|
|
res.setHeader('content-type', 'text/html').end(html)
|
|
}
|
|
|
|
export default handler
|
|
|
|
declare let __globalServer: ViteDevServer
|
|
|
|
async function importHtml() {
|
|
if (import.meta.env.DEV) {
|
|
const mod = await import('/index.html?raw')
|
|
return __globalServer.transformIndexHtml('/', mod.default)
|
|
} else {
|
|
const mod = await import('/dist/client/index.html?raw')
|
|
return mod.default
|
|
}
|
|
}
|