switch to react location for routing

This commit is contained in:
MapleLeaf
2021-12-31 15:48:57 -06:00
committed by Darius
parent f603bdd7ba
commit e64d3cbdfd
7 changed files with 96 additions and 29 deletions

View File

@@ -1,13 +1,12 @@
import { description } from "reacord/package.json"
import { Meta, Title } from "react-head"
import { Route, Routes } from "react-router"
import { Link } from "react-router-dom"
import { DocumentPage } from "./pages/document-page"
import { LandingPage } from "./pages/landing-page"
import type { ReactLocation } from "react-location"
import { Link, Outlet, Router } from "react-location"
import { routes } from "./routes"
export function App() {
export function App({ location }: { location: ReactLocation }) {
return (
<>
<Router location={location} routes={routes}>
<Title>Reacord</Title>
<Meta name="description" content={description} />
<nav>
@@ -15,10 +14,7 @@ export function App() {
<Link to="docs/getting-started">Getting Started</Link>{" "}
<Link to="docs/api">API Reference</Link>
</nav>
<Routes>
<Route path="/" element={<LandingPage />} />
<Route path="docs/*" element={<DocumentPage />} />
</Routes>
</>
<Outlet />
</Router>
)
}

View File

@@ -1,13 +1,13 @@
import { hydrate } from "react-dom"
import { HeadProvider } from "react-head"
import { BrowserRouter } from "react-router-dom"
import { ReactLocation } from "react-location"
import { App } from "./app"
const location = new ReactLocation()
hydrate(
<BrowserRouter>
<HeadProvider>
<App />
</HeadProvider>
</BrowserRouter>,
<HeadProvider>
<App location={location} />
</HeadProvider>,
document.querySelector("#app"),
)

View File

@@ -1,17 +1,19 @@
import { renderToString } from "react-dom/server"
import { HeadProvider } from "react-head"
import { StaticRouter } from "react-router-dom/server"
import { createMemoryHistory, ReactLocation } from "react-location"
import { App } from "./app"
export async function render(url: string) {
const headTags: React.ReactElement[] = []
const location = new ReactLocation({
history: createMemoryHistory({ initialEntries: [url] }),
})
const app = renderToString(
<StaticRouter location={url}>
<HeadProvider headTags={headTags}>
<App />
</HeadProvider>
</StaticRouter>,
<HeadProvider headTags={headTags}>
<App location={location} />
</HeadProvider>,
)
const scriptSource = import.meta.env.PROD

View File

@@ -1,10 +1,10 @@
import GettingStarted from "../docs/getting-started.md"
import { Outlet } from "react-location"
export function DocumentPage() {
return (
<>
<h1>Docs</h1>
<GettingStarted />
<Outlet />
</>
)
}

View File

@@ -0,0 +1,21 @@
import type { Route } from "react-location"
export const routes: Route[] = [
{
path: "/",
element: () =>
import("./pages/landing-page").then((m) => <m.LandingPage />),
},
{
path: "docs",
element: () =>
import("./pages/document-page").then((m) => <m.DocumentPage />),
children: [
{
path: "*",
element: ({ params }) =>
import(`./docs/${params["*"]}.md`).then((m) => <m.default />),
},
],
},
]