simple vite react ssr setup
This commit is contained in:
11
packages/docs-new/src/counter.tsx
Normal file
11
packages/docs-new/src/counter.tsx
Normal file
@@ -0,0 +1,11 @@
|
||||
import * as React from "react"
|
||||
|
||||
export function Counter() {
|
||||
const [count, setCount] = React.useState(0)
|
||||
return (
|
||||
<div>
|
||||
<p>You clicked {count} times!</p>
|
||||
<button onClick={() => setCount(count + 1)}>Click me</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
5
packages/docs-new/src/entry.client.tsx
Normal file
5
packages/docs-new/src/entry.client.tsx
Normal file
@@ -0,0 +1,5 @@
|
||||
import * as React from "react"
|
||||
import { render } from "react-dom"
|
||||
import { Root } from "./root"
|
||||
|
||||
render(<Root />, document)
|
||||
6
packages/docs-new/src/entry.server.tsx
Normal file
6
packages/docs-new/src/entry.server.tsx
Normal file
@@ -0,0 +1,6 @@
|
||||
import * as React from "react"
|
||||
import { Root } from "./root"
|
||||
|
||||
export async function render(url: string) {
|
||||
return <Root />
|
||||
}
|
||||
21
packages/docs-new/src/root.tsx
Normal file
21
packages/docs-new/src/root.tsx
Normal file
@@ -0,0 +1,21 @@
|
||||
import React from "react"
|
||||
import { Counter } from "./counter"
|
||||
|
||||
export function Root() {
|
||||
return (
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charSet="UTF-8" />
|
||||
<meta httpEquiv="X-UA-Compatible" content="IE=edge" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="description" content="test description" />
|
||||
<title>Reacord</title>
|
||||
<script type="module" src="/src/entry.client.tsx" />
|
||||
</head>
|
||||
<body>
|
||||
<h1>hi</h1>
|
||||
<Counter />
|
||||
</body>
|
||||
</html>
|
||||
)
|
||||
}
|
||||
35
packages/docs-new/src/server.tsx
Normal file
35
packages/docs-new/src/server.tsx
Normal file
@@ -0,0 +1,35 @@
|
||||
import express from "express"
|
||||
import { renderToStaticMarkup } from "react-dom/server"
|
||||
import { createServer as createViteServer } from "vite"
|
||||
import type * as entryModule from "./entry.server"
|
||||
|
||||
const vite = await createViteServer({
|
||||
server: { middlewareMode: "ssr" },
|
||||
})
|
||||
|
||||
const app = express()
|
||||
|
||||
app.use(vite.middlewares)
|
||||
|
||||
app.use("*", async (req, res) => {
|
||||
const url = req.originalUrl
|
||||
|
||||
try {
|
||||
const { render } = (await vite.ssrLoadModule(
|
||||
"/src/entry.server.tsx",
|
||||
)) as typeof entryModule
|
||||
|
||||
const content = renderToStaticMarkup(await render(url))
|
||||
const html = await vite.transformIndexHtml(url, content)
|
||||
res.status(200).set({ "Content-Type": "text/html" }).end(html)
|
||||
} catch (error: any) {
|
||||
vite.ssrFixStacktrace(error)
|
||||
console.error(error)
|
||||
res.status(500).end(error.message)
|
||||
}
|
||||
})
|
||||
|
||||
const port = process.env.PORT || 3000
|
||||
app.listen(port, () => {
|
||||
console.log(`listening on http://localhost:${port}`)
|
||||
})
|
||||
Reference in New Issue
Block a user