Skip to content

Commit

Permalink
Fix subrouter createURL (#154)
Browse files Browse the repository at this point in the history
* Fix subrouter createURL

* Check if we are on root router on client and server side

* restore LangService

* rename file routers

* Rename ROUTERS to Routers

* Add test

* Add test

* Manage readme

* Big change on createUrl -> internalize logic

* Remove compileUrl, use compile from path-to-regexp instead
  • Loading branch information
Willy Brauner authored Nov 9, 2023
1 parent d4a538a commit 69f840a
Show file tree
Hide file tree
Showing 21 changed files with 2,889 additions and 1,501 deletions.
3 changes: 3 additions & 0 deletions examples/example-ssr/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ const port = portFinderSync.getPort(3000)
// use vite's connect instance as middleware
app.use(vite.middlewares)
app.use("*", async (req, res, next) => {
if (req.originalUrl === "/favicon.ico") return

try {
// Transforms the ESM source code to be usable in Node.js
const { render } = await vite.ssrLoadModule(`src/server/index-server.tsx`)
// Get react-dom from the render method

const dom = await render(req.originalUrl, devScripts, false)
// Create stream to support Suspense API
const stream = renderToPipeableStream(dom, {
Expand Down
2 changes: 2 additions & 0 deletions examples/example-ssr/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ hydrateRoot(
history={createBrowserHistory()}
initialStaticProps={window["__SSR_STATIC_PROPS__"]}
langService={langServiceInstance()}
base={import.meta.env.VITE_APP_BASE || "/"}
id={1}
>
<GlobalDataContext.Provider value={window["__GLOBAL_DATA__"]}>
<App />
Expand Down
5 changes: 4 additions & 1 deletion examples/example-ssr/src/langServiceInstance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { languages, showDefaultLangInUrl } from "./languages"
import { LangService } from "@cher-ami/router"

export const langServiceInstance = (base = "/", url = window.location.pathname) =>
export const langServiceInstance = (
base = import.meta.env.VITE_APP_BASE || "/",
url = window.location.pathname,
) =>
new LangService({
showDefaultLangInUrl,
staticLocation: url,
Expand Down
8 changes: 6 additions & 2 deletions examples/example-ssr/src/pages/BarPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useRef } from "react"
import { useStack } from "@cher-ami/router"
import { transitionsHelper } from "../helpers/transitionsHelper"
import { Link, useStack } from "@cher-ami/router"
import { transitionsHelper } from "~/helpers/transitionsHelper"
import { EPages } from "~/routes"

const componentName = "BarPage"
function BarPage(props, handleRef) {
Expand All @@ -17,6 +18,9 @@ function BarPage(props, handleRef) {
return (
<div className={[componentName].filter((e) => e).join(" ")} ref={rootRef}>
{componentName}
<br />
<br />
<Link to={{ name: EPages.FOO }}>link to FOO</Link>
</div>
)
}
Expand Down
6 changes: 5 additions & 1 deletion examples/example-ssr/src/server/index-server.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ export async function render(
scripts: TScriptsObj,
isPrerender = false
): Promise<ReactElement> {

if (url === "/@vite-plugin-checker-runtime") return

// prepare base & URL
const base = process.env.VITE_APP_BASE || loadEnv("", process.cwd(), "").VITE_APP_BASE || '/'
url = preventSlashes(`${isPrerender ? base : ""}${url}`)
Expand All @@ -41,7 +44,7 @@ export async function render(


// Request static props
const ssrStaticProps = await requestStaticPropsFromRoute({ url, base, routes, langService })
const ssrStaticProps = await requestStaticPropsFromRoute({ url, base, routes, langService, id:"1-RSP" })
const meta = ssrStaticProps?.props?.meta

// Request Global data example-client
Expand Down Expand Up @@ -75,6 +78,7 @@ export async function render(
langService={langService}
staticLocation={url}
initialStaticProps={ssrStaticProps}
id={1}
>
<GlobalDataContext.Provider value={globalData}>
<App />
Expand Down
Loading

0 comments on commit 69f840a

Please sign in to comment.