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 geStaticProps first route visited check #119

Merged
merged 1 commit into from
Sep 13, 2022
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
1 change: 1 addition & 0 deletions examples/example-ssr/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function App() {
<Link to={{ name: "Home" }}>Home</Link> |{" "}
<Link to={{ name: "About" }}>About</Link> |{" "}
<Link to={{ name: "Article", params: { slug: "article-1" } }}>Article 1</Link> |{" "}
<Link to={{ name: "Article", params: { slug: "article-2" } }}>Article 2</Link> |{" "}
<Link to={{ name: "Contact" }}>Contact</Link>
</nav>
<Stack manageTransitions={crossedTransitions} />
Expand Down
7 changes: 7 additions & 0 deletions examples/example-ssr/src/pages/ArticlePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ const componentName = "ArticlePage"
function ArticlePage(props, handleRef) {
const rootRef = useRef(null)


useEffect(()=>
{
console.log("mySlug",props.mySlug)
}, [props.mySlug])


useStack({
componentName,
handleRef,
Expand Down
5 changes: 4 additions & 1 deletion examples/example-ssr/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export const routes: TRoute[] = [
getStaticProps: async (props) => {
const res = await fetch("https://jsonplaceholder.typicode.com/todos");
const todo = await res.json();
return { todo };

const mySlug = props.params.slug

return { todo, mySlug };
},
},
{
Expand Down
8 changes: 5 additions & 3 deletions src/components/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function Router(props: {
middlewares?: ((routes: TRoute[]) => TRoute[])[];
langService?: LangService;
id?: number | string;
initialStaticProps?: { props: any; name: string };
initialStaticProps?: { props: any; name: string, url: string };
}): JSX.Element {
/**
* 0. LangService
Expand Down Expand Up @@ -271,15 +271,17 @@ function Router(props: {
const dataFromCache = cache.get(newRoute._fullUrl);

// first route visited (server & client)
const isFirstRouteVisited = newRoute.name === props.initialStaticProps.name;
const isFirstRouteVisited = newRoute._fullUrl === props.initialStaticProps.url;
log("is first route visited?",isFirstRouteVisited)

// In SSR context, we have to manage getStaticProps route properties from server and client
if (isFirstRouteVisited) {
log("newRoute.props",newRoute.props)
if (newRoute.props) {
Object.assign(newRoute.props, props.initialStaticProps?.props ?? {});
}
if (!dataFromCache) {
cache.set(newRoute._fullUrl, props.initialStaticProps?.props ?? {});
cache.set(newRoute._fullUrl, newRoute.props ?? {});
}
}
// if NOT first route (client)
Expand Down
6 changes: 5 additions & 1 deletion src/core/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ describe("public", () => {
base: "/",
routes: routeList,
});
expect(ssrStaticProps).toEqual({ props: { fetchData: {} }, name: "HelloPage" });
expect(ssrStaticProps).toEqual({
props: { fetchData: {} },
name: "HelloPage",
url: "/hello",
});
});
});
});
Expand Down
3 changes: 2 additions & 1 deletion src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export async function requestStaticPropsFromRoute({
langService?: LangService;
middlewares?: ((routes: TRoute[]) => TRoute[])[];
id?: string | number;
}): Promise<{ props: any; name: string }> {
}): Promise<{ props: any; name: string; url: string }> {
const currentRoute = getRouteFromUrl({
pUrl: url,
pBase: base,
Expand All @@ -205,6 +205,7 @@ export async function requestStaticPropsFromRoute({
const SSR_STATIC_PROPS = {
props: null,
name: currentRoute.name,
url,
};

// await promise from getStaticProps
Expand Down