Skip to content

Commit c157c97

Browse files
committed
add router for check ssr in sub path
1 parent 582654a commit c157c97

File tree

8 files changed

+187
-30
lines changed

8 files changed

+187
-30
lines changed

dist/bundle.js

Lines changed: 82 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
"@babel/core": "^7.25.2",
1616
"@babel/preset-env": "^7.25.3",
1717
"@babel/preset-react": "^7.24.7",
18+
"@remix-run/router": "^1.19.1",
1819
"babel-loader": "^9.1.3",
1920
"express": "^4.19.2",
2021
"react": "^18.3.1",
2122
"react-dom": "^18.3.1",
23+
"react-router-dom": "^6.26.1",
2224
"webpack": "^5.93.0",
2325
"webpack-cli": "^5.1.4",
2426
"webpack-dev-middleware": "^7.3.0",

server.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
1-
const express = require('express');
2-
const React = require('react');
3-
const ReactDOMServer = require('react-dom/server');
4-
const path = require('path');
5-
const App = require('./src/App').default;
1+
import express from 'express';
2+
import path from 'path';
3+
import fs from 'fs';
4+
import React from 'react';
5+
import ReactDOMServer from 'react-dom/server';
6+
import { StaticRouter } from 'react-router-dom/server';
7+
import App from './src/App';
68

79
const app = express();
810

9-
// 提供打包后的静态文件
1011
app.use(express.static(path.resolve(__dirname, 'dist')));
1112

1213
app.get('*', (req, res) => {
13-
const appHtml = ReactDOMServer.renderToString(React.createElement(App));
14+
const context = {};
15+
16+
const appHtml = ReactDOMServer.renderToString(
17+
<StaticRouter location={req.url} context={context}>
18+
<App />
19+
</StaticRouter>
20+
);
21+
22+
// const indexFile = path.resolve(__dirname, 'dist', 'index.html');
23+
// fs.readFile(indexFile, 'utf8', (err, data) => {
24+
// if (err) {
25+
// console.error('Error reading index.html', err);
26+
// return res.status(500).send('Some error happened');
27+
// }
28+
29+
// return res.send(
30+
// data.replace('<div id="root"></div>', `<div id="root">${appHtml}</div>`)
31+
// );
32+
// });
1433
const html = `
1534
<!DOCTYPE html>
1635
<html lang="en">
@@ -29,5 +48,5 @@ app.get('*', (req, res) => {
2948
});
3049

3150
app.listen(3000, () => {
32-
console.log('Server is running on http://localhost:3000');
51+
console.log('Server is listening on port 3000');
3352
});

src/App.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
// src/App.js
21
import React from 'react';
2+
import { Routes, Route, Link } from 'react-router-dom';
3+
import Home from './Home';
4+
import Blog from './Blog';
35

46
function App() {
57
return (
68
<div>
7-
<h1>Hello, SSR!</h1>
8-
<p>This is a simple server-side rendered React application.</p>
9+
<nav>
10+
<Link to="/">Home</Link>
11+
<Link to="/blog/1">Blog 1</Link>
12+
</nav>
13+
<Routes>
14+
<Route path="/" element={<Home />} />
15+
<Route path="/blog/:id" element={<Blog />} />
16+
</Routes>
917
</div>
1018
);
1119
}
1220

1321
export default App;
14-

src/Blog.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import { useParams } from 'react-router-dom';
3+
4+
function Blog() {
5+
const { id } = useParams();
6+
return <h1>Blog Page with ID: {id}</h1>;
7+
}
8+
9+
export default Blog;

src/Home.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
3+
function Home() {
4+
return <h1>Home Page</h1>;
5+
}
6+
7+
export default Home;

src/index.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
import React, { useState } from 'react';
2-
import { hydrateRoot } from 'react-dom/client';
1+
import React from 'react';
2+
import ReactDOM from 'react-dom/client';
3+
import { BrowserRouter } from 'react-router-dom';
34
import App from './App';
45

5-
function HydrationTest() {
6-
const [clicked, setClicked] = useState(false);
7-
8-
return (
9-
<div>
10-
<App />
11-
<button onClick={() => setClicked(!clicked)}>
12-
{clicked ? 'Clicked!' : 'Click me'}
13-
</button>
14-
</div>
15-
);
16-
}
17-
18-
hydrateRoot(document.getElementById('root'), <HydrationTest />);
6+
const root = ReactDOM.createRoot(document.getElementById('root'));
7+
root.render(
8+
<BrowserRouter>
9+
<App />
10+
</BrowserRouter>
11+
);

0 commit comments

Comments
 (0)