Skip to content

Commit

Permalink
test: rewrite playground/ssr-react without react-router (#398)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Dec 27, 2024
1 parent ad9e001 commit f099656
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 52 deletions.
3 changes: 1 addition & 2 deletions playground/ssr-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
},
"dependencies": {
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.28.0"
"react-dom": "^18.3.1"
},
"devDependencies": {
"@vitejs/plugin-react": "workspace:*",
Expand Down
Binary file added playground/ssr-react/public/favicon.ico
Binary file not shown.
59 changes: 51 additions & 8 deletions playground/ssr-react/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Link, Route, Routes } from 'react-router-dom'
import React from 'react'

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

export function App() {
function NotFound() {
return <h1>Not found</h1>
}

/**
* @param {{ url: URL }} props
*/
export function App(props) {
const [url, setUrl] = React.useState(props.url)

React.useEffect(() => {
return listenNavigation(() => {
setUrl(new URL(window.location.href))
})
}, [setUrl])

const route = routes.find((route) => route.path === url.pathname)
const Component = route?.component ?? NotFound
return (
<>
<nav>
<ul>
{routes.map(({ name, path }) => {
return (
<li key={path}>
<Link to={path}>{name}</Link>
<a href={path}>{name}</a>
</li>
)
})}
</ul>
</nav>
<Routes>
{routes.map(({ path, component: RouteComp }) => {
return <Route key={path} path={path} element={<RouteComp />}></Route>
})}
</Routes>
<Component />
</>
)
}

/**
* @param {() => void} onNavigation
*/
function listenNavigation(onNavigation) {
/**
* @param {MouseEvent} e
*/
function onClick(e) {
let link = e.target.closest('a')
if (
link &&
link instanceof HTMLAnchorElement &&
link.href &&
(!link.target || link.target === '_self') &&
link.origin === location.origin &&
!link.hasAttribute('download') &&
e.button === 0 &&
!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey)
) {
e.preventDefault()
history.pushState(null, '', link.href)
onNavigation()
}
}
document.addEventListener('click', onClick)
return () => {
document.removeEventListener('click', onClick)
}
}
5 changes: 1 addition & 4 deletions playground/ssr-react/src/entry-client.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import ReactDOM from 'react-dom/client'
import { BrowserRouter } from 'react-router-dom'
import { App } from './App'

ReactDOM.hydrateRoot(
document.getElementById('app'),
<BrowserRouter>
<App />
</BrowserRouter>,
<App url={new URL(window.location.href)} />,
)
console.log('hydrated')
5 changes: 1 addition & 4 deletions playground/ssr-react/src/entry-server.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import ReactDOMServer from 'react-dom/server'
import { StaticRouter } from 'react-router-dom/server'
import { App } from './App'

export function render(url) {
return ReactDOMServer.renderToString(
<StaticRouter location={url}>
<App />
</StaticRouter>,
<App url={new URL(url, 'http://localhost')} />,
)
}
34 changes: 0 additions & 34 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f099656

Please sign in to comment.