From cdf043995a7701141a0c0aee41fedeb649432580 Mon Sep 17 00:00:00 2001 From: Marc Benito Date: Thu, 25 Jul 2024 12:43:03 +0200 Subject: [PATCH 1/2] feat(packages/sui-react-initial-props): Add shallow property --- .../src/withInitialProps.tsx | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/packages/sui-react-initial-props/src/withInitialProps.tsx b/packages/sui-react-initial-props/src/withInitialProps.tsx index f1d44a734..da85a84fa 100644 --- a/packages/sui-react-initial-props/src/withInitialProps.tsx +++ b/packages/sui-react-initial-props/src/withInitialProps.tsx @@ -58,35 +58,38 @@ export default (Page: ClientPageComponent): WithInitialPropsComponent => { isLoading: initialPropsFromWindowRef.current == null })) + const avoidReRender = Boolean(routeInfo.location.state?.shallow) || false useEffect(() => { // check if got initial props from window, because then there's no need // to request them again from client if (initialPropsFromWindowRef.current != null) { initialPropsFromWindowRef.current = undefined } else { - // only update state if already request initial props - if (requestedInitialPropsOnceRef.current) { - setState({initialProps, isLoading: true}) + if (!avoidReRender) { + // only update state if already request initial props + if (requestedInitialPropsOnceRef.current) { + setState({initialProps, isLoading: true}) + } + + Page.getInitialProps({context, routeInfo}) + .then((initialProps: InitialProps) => { + const {__HTTP__: http} = initialProps + + if (http?.redirectTo !== undefined) { + window.location = http.redirectTo + return + } + + setState({initialProps, isLoading: false}) + }) + .catch((error: Error) => { + setState({initialProps: {error}, isLoading: false}) + }) + .finally(() => { + if (requestedInitialPropsOnceRef.current) return + requestedInitialPropsOnceRef.current = true + }) } - - Page.getInitialProps({context, routeInfo}) - .then((initialProps: InitialProps) => { - const {__HTTP__: http} = initialProps - - if (http?.redirectTo !== undefined) { - window.location = http.redirectTo - return - } - - setState({initialProps, isLoading: false}) - }) - .catch((error: Error) => { - setState({initialProps: {error}, isLoading: false}) - }) - .finally(() => { - if (requestedInitialPropsOnceRef.current) return - requestedInitialPropsOnceRef.current = true - }) } }, [routeInfo.location]) // eslint-disable-line react-hooks/exhaustive-deps @@ -94,7 +97,7 @@ export default (Page: ClientPageComponent): WithInitialPropsComponent => { // if the page has a `keepMounted` property and already requested // initialProps once, just keep rendering the page - if (keepMounted && requestedInitialPropsOnceRef.current) { + if (keepMounted && !avoidReRender && requestedInitialPropsOnceRef.current) { return renderPage() } @@ -108,7 +111,7 @@ export default (Page: ClientPageComponent): WithInitialPropsComponent => { // if `keepMounted` property is found and the component is the same one, // we just reuse it instead of returning a new one - if (keepMounted && Page.displayName === latestClientPage?.Page?.displayName) { + if (Page.displayName === latestClientPage?.Page?.displayName) { return latestClientPage } From 14fe748ff21e4911f588f8fe608e9ec0fcb36339 Mon Sep 17 00:00:00 2001 From: Marc Benito Date: Thu, 25 Jul 2024 12:43:31 +0200 Subject: [PATCH 2/2] feat(packages/sui-react-router): Add shallow typing --- packages/sui-react-router/src/types.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/sui-react-router/src/types.d.ts b/packages/sui-react-router/src/types.d.ts index 232f6d796..660e7fbab 100644 --- a/packages/sui-react-router/src/types.d.ts +++ b/packages/sui-react-router/src/types.d.ts @@ -13,7 +13,8 @@ export type Search = string export type Path = string interface LocationState { - from: Location + from?: Location + shallow?: Boolean } export interface LocationDescriptorObject {