Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix subrouter createURL #154

Merged
merged 10 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading