Merge pull request #3 from azpha/ssr

Ssr
This commit is contained in:
Alex Frantz
2025-02-05 20:28:58 -05:00
committed by GitHub
12 changed files with 1349 additions and 364 deletions
+2 -2
View File
@@ -29,7 +29,7 @@
<title>Alex Frantz</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
<div id="app"><!--app-html--></div>
<script type="module" src="/src/entry-client.tsx"></script>
</body>
</html>
+1217 -299
View File
File diff suppressed because it is too large Load Diff
+10 -6
View File
@@ -4,29 +4,33 @@
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview",
"start": "tsc && vite build && serve -p 8099 -s ./dist"
"build": "npm run build:client && npm run build:server",
"build:client": "vite build --outDir dist/client",
"build:server": "cross-env SSR=true vite build --outDir dist/server"
},
"dependencies": {
"@types/express": "^5.0.0",
"@types/node": "^22.13.1",
"compression": "^1.7.5",
"express": "^4.21.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.26.2"
},
"devDependencies": {
"@types/compression": "^1.7.5",
"@types/react": "^18.2.66",
"@types/react-dom": "^18.2.22",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"@vitejs/plugin-react": "^4.2.1",
"@vitejs/plugin-react": "^4.3.4",
"autoprefixer": "^10.4.19",
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.6",
"postcss": "^8.4.38",
"tailwindcss": "^3.4.3",
"ts-node": "^10.9.2",
"typescript": "^5.2.2",
"vite": "^5.2.0"
}
+44
View File
@@ -0,0 +1,44 @@
import express, { Request, Response } from 'express'
import { createServer as createViteServer } from 'vite'
import compression from 'compression'
import fs from 'fs'
import path from 'path'
async function createServer() {
const app = express()
app.use(compression())
const vite = await createViteServer({
server: { middlewareMode: true },
appType: 'custom'
})
app.use(vite.middlewares)
app.use('*', async (req: Request, res: Response) => {
const url = req.originalUrl
try {
const template = fs.readFileSync(
path.resolve(process.cwd(), 'index.html'),
'utf-8'
)
const transformedTemplate = await vite.transformIndexHtml(url, template)
const { render } = await vite.ssrLoadModule('./src/entry-server.tsx')
const appHtml = await render(url)
const html = transformedTemplate.replace(`<!--app-html-->`, appHtml)
res.status(200).set({ 'Content-Type': 'text/html' }).end(html)
} catch (e) {
vite.ssrFixStacktrace(e as Error)
console.error(e)
res.status(500).end((e as Error).stack)
}
})
app.listen(3000, () => {
console.log('server started')
})
}
createServer()
+21
View File
@@ -0,0 +1,21 @@
import { Routes, Route } from 'react-router-dom'
import MainPage from './pages/index';
import PrivacyPage from './pages/privacy';
import MusicWidget from './pages/music';
import LinksPage from './pages/links';
import NotFound from './pages/NotFound';
import './assets/main.css'
export default function App() {
return (
<Routes>
<Route path="/" element={<MainPage />} />
<Route path="/privacy" element={<PrivacyPage />} />
<Route path="/music" element={<MusicWidget />} />
<Route path="/links" element={<LinksPage />} />
<Route path="*" element={<NotFound />} />
</Routes>
)
}
+4 -1
View File
@@ -20,8 +20,11 @@ export default function Header({
"where else would you rather be..",
"..than right here, right now?!"
]
const [ quote, setQuote ] = useState<string>(quoteArray[Math.floor(Math.random() * quoteArray.length)]);
const [ quote, setQuote ] = useState<string>(quoteArray[0]);
useEffect(() => {
setQuote(quoteArray[Math.floor(Math.random() * quoteArray.length)])
}, [])
const drawNewQuote = () => {
const newQuote = quoteArray[Math.floor(Math.random() * quoteArray.length)]
+10
View File
@@ -0,0 +1,10 @@
import ReactDOM from "react-dom/client";
import { BrowserRouter } from "react-router-dom";
import App from './App'
ReactDOM.hydrateRoot(
document.getElementById("app") as HTMLElement,
<BrowserRouter>
<App />
</BrowserRouter>
)
+11
View File
@@ -0,0 +1,11 @@
import { StaticRouter } from "react-router-dom/server"
import { renderToString } from "react-dom/server"
import App from "./App"
export function render(url: string): string {
return renderToString(
<StaticRouter location={url}>
<App />
</StaticRouter>
)
}
-35
View File
@@ -1,35 +0,0 @@
import ReactDOM from 'react-dom/client'
import {
createBrowserRouter,
RouterProvider
} from "react-router-dom"
import './assets/main.css'
// pages
import MainPage from './pages/index';
import PrivacyPage from './pages/privacy';
import MusicWidget from './pages/music';
import LinksPage from './pages/links';
const router = createBrowserRouter([
{
path: '/',
element: <MainPage />
},
{
path: '/privacy',
element: <PrivacyPage />
},
{
path: '/music',
element: <MusicWidget />
},
{
path: '/links',
element: <LinksPage />
}
])
ReactDOM.createRoot(document.getElementById('root')!).render(
<RouterProvider router={router} />
)
+14
View File
@@ -0,0 +1,14 @@
import Layout from "../components/Layout";
export default function NotFound() {
return (
<Layout>
<div className="min-h-screen flex justify-center items-center">
<div className="text-center pb-28">
<h1 className="text-2xl font-bold">well that sucks</h1>
<p>that page doesn't exist. skill issue</p>
<a href="#" className="underline" onClick={() => history.back()}>go back?</a>
</div>
</div>
</Layout>
)
}
+6 -18
View File
@@ -1,25 +1,13 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"target": "ESNext",
"module": "ESNext",
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"moduleResolution": "node",
"jsx": "react-jsx",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
"esModuleInterop": true,
"skipLibCheck": true,
"types": ["vite/client", "node"]
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
"include": ["src"]
}
+9 -2
View File
@@ -1,8 +1,15 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
publicDir: "./src/public"
publicDir: './src/public',
build: {
copyPublicDir: !!process.env.SSR,
ssr: !!process.env.SSR,
outDir: process.env.SSR ? 'dist/server' : 'dist/client',
rollupOptions: {
input: process.env.SSR ? './server.ts' : './index.html'
}
}
})