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

Feat/poc shallow routing ts #1793

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
51 changes: 27 additions & 24 deletions packages/sui-react-initial-props/src/withInitialProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,43 +58,46 @@ 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

const renderPage = (): any => <Page {...initialProps} {...props} isLoading={isLoading} />

// 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()
}

Expand All @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion packages/sui-react-router/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export type Search = string
export type Path = string

interface LocationState {
from: Location
from?: Location
shallow?: Boolean
}

export interface LocationDescriptorObject {
Expand Down
Loading