update
This commit is contained in:
@@ -1,31 +1,86 @@
|
||||
"use server";
|
||||
import { renderToPipeableStream, renderToString } from 'react-dom/server';
|
||||
// import A from './pages/a/index.tsx';
|
||||
import { AEntry } from './browser-entry.tsx';
|
||||
import AServer from './pages/a/server/index.tsx';
|
||||
import { renderToReadableStream } from 'react-dom/server';
|
||||
import Main from './pages/a/main.tsx';
|
||||
import http from 'http';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
const PORT = 3000;
|
||||
const distDir = path.join(process.cwd(), 'dist');
|
||||
const indexHtmlPath = path.join(process.cwd(), 'dist/index.html');
|
||||
|
||||
const mimeTypes: Record<string, string> = {
|
||||
'.html': 'text/html',
|
||||
'.js': 'application/javascript',
|
||||
'.css': 'text/css',
|
||||
'.json': 'application/json',
|
||||
'.png': 'image/png',
|
||||
'.jpg': 'image/jpeg',
|
||||
'.svg': 'image/svg+xml',
|
||||
'.ico': 'image/x-icon',
|
||||
};
|
||||
|
||||
async function ssrRender(res: http.ServerResponse, version: string) {
|
||||
let template: string;
|
||||
try {
|
||||
template = fs.readFileSync(indexHtmlPath, 'utf-8');
|
||||
} catch {
|
||||
res.writeHead(500);
|
||||
res.end('dist/index.html not found');
|
||||
return;
|
||||
}
|
||||
|
||||
const serverData = { version };
|
||||
|
||||
try {
|
||||
const stream = await renderToReadableStream(<Main />, {
|
||||
bootstrapScripts: []
|
||||
});
|
||||
|
||||
let renderedHtml = '';
|
||||
for await (const chunk of stream) {
|
||||
renderedHtml += new TextDecoder().decode(chunk);
|
||||
}
|
||||
|
||||
console.log('Rendered HTML:', renderedHtml);
|
||||
const dataScript = `<script>window.__SERVER_DATA__ = ${JSON.stringify(serverData)};</script>`;
|
||||
const html = template
|
||||
.replace('<div id="root"></div>', `<div id="root">${renderedHtml}</div>`)
|
||||
.replace('</head>', `${dataScript}</head>`);
|
||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||
res.end(html);
|
||||
} catch (err) {
|
||||
console.error('Render error:', err);
|
||||
res.writeHead(500);
|
||||
res.end('Server Error');
|
||||
}
|
||||
}
|
||||
|
||||
http.createServer((req, res) => {
|
||||
if (req.url === '/ssr') {
|
||||
const { pipe } = renderToPipeableStream(<AServer />, {
|
||||
bootstrapScripts: [],
|
||||
onShellReady() {
|
||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||
pipe(res);
|
||||
},
|
||||
onShellError(err) {
|
||||
console.error('Shell error:', err);
|
||||
res.writeHead(500);
|
||||
res.end('Server Error');
|
||||
}
|
||||
});
|
||||
} else {
|
||||
const str = renderToString(<A />);
|
||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||
res.end(str);
|
||||
const urlPath = req.url?.split('?')[0] || '/';
|
||||
|
||||
if (urlPath === '/') {
|
||||
ssrRender(res, '');
|
||||
return;
|
||||
}
|
||||
|
||||
if (urlPath === '/a') {
|
||||
ssrRender(res, '1.0.0');
|
||||
return;
|
||||
}
|
||||
|
||||
const distFilePath = path.join(distDir, urlPath);
|
||||
try {
|
||||
if (fs.existsSync(distFilePath) && fs.statSync(distFilePath).isFile()) {
|
||||
const ext = path.extname(distFilePath);
|
||||
const contentType = mimeTypes[ext] || 'application/octet-stream';
|
||||
res.writeHead(200, { 'Content-Type': contentType });
|
||||
fs.createReadStream(distFilePath).pipe(res);
|
||||
return;
|
||||
}
|
||||
} catch {
|
||||
}
|
||||
|
||||
ssrRender(res, '');
|
||||
}).listen(PORT, () => {
|
||||
console.log(`Server running on http://localhost:${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user