Skip to content

Commit f099656

Browse files
authored
test: rewrite playground/ssr-react without react-router (#398)
1 parent ad9e001 commit f099656

File tree

6 files changed

+54
-52
lines changed

6 files changed

+54
-52
lines changed

playground/ssr-react/package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
},
1414
"dependencies": {
1515
"react": "^18.3.1",
16-
"react-dom": "^18.3.1",
17-
"react-router-dom": "^6.28.0"
16+
"react-dom": "^18.3.1"
1817
},
1918
"devDependencies": {
2019
"@vitejs/plugin-react": "workspace:*",
4.19 KB
Binary file not shown.

playground/ssr-react/src/App.jsx

+51-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Link, Route, Routes } from 'react-router-dom'
1+
import React from 'react'
22

33
// Auto generates routes from files under ./pages
44
// https://vitejs.dev/guide/features.html#glob-import
@@ -13,25 +13,68 @@ const routes = Object.keys(pages).map((path) => {
1313
}
1414
})
1515

16-
export function App() {
16+
function NotFound() {
17+
return <h1>Not found</h1>
18+
}
19+
20+
/**
21+
* @param {{ url: URL }} props
22+
*/
23+
export function App(props) {
24+
const [url, setUrl] = React.useState(props.url)
25+
26+
React.useEffect(() => {
27+
return listenNavigation(() => {
28+
setUrl(new URL(window.location.href))
29+
})
30+
}, [setUrl])
31+
32+
const route = routes.find((route) => route.path === url.pathname)
33+
const Component = route?.component ?? NotFound
1734
return (
1835
<>
1936
<nav>
2037
<ul>
2138
{routes.map(({ name, path }) => {
2239
return (
2340
<li key={path}>
24-
<Link to={path}>{name}</Link>
41+
<a href={path}>{name}</a>
2542
</li>
2643
)
2744
})}
2845
</ul>
2946
</nav>
30-
<Routes>
31-
{routes.map(({ path, component: RouteComp }) => {
32-
return <Route key={path} path={path} element={<RouteComp />}></Route>
33-
})}
34-
</Routes>
47+
<Component />
3548
</>
3649
)
3750
}
51+
52+
/**
53+
* @param {() => void} onNavigation
54+
*/
55+
function listenNavigation(onNavigation) {
56+
/**
57+
* @param {MouseEvent} e
58+
*/
59+
function onClick(e) {
60+
let link = e.target.closest('a')
61+
if (
62+
link &&
63+
link instanceof HTMLAnchorElement &&
64+
link.href &&
65+
(!link.target || link.target === '_self') &&
66+
link.origin === location.origin &&
67+
!link.hasAttribute('download') &&
68+
e.button === 0 &&
69+
!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey)
70+
) {
71+
e.preventDefault()
72+
history.pushState(null, '', link.href)
73+
onNavigation()
74+
}
75+
}
76+
document.addEventListener('click', onClick)
77+
return () => {
78+
document.removeEventListener('click', onClick)
79+
}
80+
}
+1-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import ReactDOM from 'react-dom/client'
2-
import { BrowserRouter } from 'react-router-dom'
32
import { App } from './App'
43

54
ReactDOM.hydrateRoot(
65
document.getElementById('app'),
7-
<BrowserRouter>
8-
<App />
9-
</BrowserRouter>,
6+
<App url={new URL(window.location.href)} />,
107
)
118
console.log('hydrated')
+1-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import ReactDOMServer from 'react-dom/server'
2-
import { StaticRouter } from 'react-router-dom/server'
32
import { App } from './App'
43

54
export function render(url) {
65
return ReactDOMServer.renderToString(
7-
<StaticRouter location={url}>
8-
<App />
9-
</StaticRouter>,
6+
<App url={new URL(url, 'http://localhost')} />,
107
)
118
}

pnpm-lock.yaml

-34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)