Skip to content

fix: prevent returning the previous route data on route transition #646

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

Merged
merged 2 commits into from
Mar 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
56 changes: 47 additions & 9 deletions packages/tuono-router/src/components/RouteMatch.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,39 @@ import { useServerPayloadData } from '../hooks/useServerPayloadData'

import { RouteMatch } from './RouteMatch'

function createRouteComponent(
routeType: string,
includeChildren: boolean,
): RouteComponent {
function createRouteComponent(routeType: string): RouteComponent {
const RootComponent = (({ children }: RouteProps) => (
<div data-testid={routeType}>
{`${routeType} route`}
{includeChildren ? children : null}
{children}
</div>
)) as RouteComponent
RootComponent.preload = vi.fn()
RootComponent.displayName = routeType
return RootComponent
}

function createLeafRouteComponent(routeType: string): RouteComponent {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From @jacobhq review comment #646 (review)

Looks good, although I don't understand why we need the createLeafRouteComponent function, would you mind explaining?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense having them separate, since passing data creates a weird HTML output that is weird dealing with.

const RootComponent = (({ data }: RouteProps) => (
<div data-testid={routeType}>{data ? JSON.stringify(data) : null}</div>
)) as RouteComponent
RootComponent.preload = vi.fn()
RootComponent.displayName = routeType
return RootComponent
}

const root = new Route({
isRoot: true,
component: createRouteComponent('root', true),
component: createRouteComponent('root'),
})

const parent = new Route({
component: createRouteComponent('parent', true),
component: createRouteComponent('parent'),
getParentRoute: (): Route => root,
})

const route = new Route({
component: createRouteComponent('current', false),
component: createLeafRouteComponent('current'),
getParentRoute: (): Route => parent,
})

Expand All @@ -49,6 +55,11 @@ describe('<RouteMatch />', () => {
afterEach(cleanup)

it('should correctly render nested routes', () => {
vi.mocked(useServerPayloadData).mockReturnValue({
data: { some: 'data' },
isLoading: false,
})

render(<RouteMatch route={route} serverInitialData={{}} />)

expect(screen.getByTestId('root')).toMatchInlineSnapshot(
Expand All @@ -64,11 +75,38 @@ describe('<RouteMatch />', () => {
<div
data-testid="current"
>
current route
{"some":"data"}
</div>
</div>
</div>
`,
)
})

it('should return null data when while loading', () => {
vi.mocked(useServerPayloadData).mockReturnValue({
data: { some: 'data' },
isLoading: true,
})

render(<RouteMatch route={route} serverInitialData={{}} />)

expect(screen.getByTestId('root')).toMatchInlineSnapshot(
`
<div
data-testid="root"
>
root route
<div
data-testid="parent"
>
parent route
<div
data-testid="current"
/>
</div>
</div>
`,
)
})
})
10 changes: 8 additions & 2 deletions packages/tuono-router/src/components/RouteMatch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ export const RouteMatch = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
const routes = useMemo(() => loadParentComponents(route), [route.id])

const routeData = isLoading ? null : data

return (
<TraverseRootComponents routes={routes} data={data} isLoading={isLoading}>
<TraverseRootComponents
routes={routes}
data={routeData}
isLoading={isLoading}
>
<Suspense>
<route.component data={data} isLoading={isLoading} />
<route.component data={routeData} isLoading={isLoading} />
</Suspense>
</TraverseRootComponents>
)
Expand Down
Loading