T
TanStack8mo ago
fascinating-indigo

Layout component in route vs using a layout route performance

Does having a layout route make it more performant to move from one page to the other compared to just having the two routes and inside each the content is wrapped by a Layout component? I'd guess there isn't much of a difference since Tanstack Start does client side routing but maybe I'm missing something.
19 Replies
fascinating-indigo
fascinating-indigoOP8mo ago
I'm asking because I have 4 different layouts and the URL structure often doesn't reflect the way layouts are used so it seems like for me it's actually better if I just have in-route layouts instead of having thousands of groupings in the routes folder. Wondering if there are any penalties to doing that.
foreign-sapphire
foreign-sapphire8mo ago
what kind of performance ?
fascinating-indigo
fascinating-indigoOP8mo ago
React re-rendering performance
foreign-sapphire
foreign-sapphire8mo ago
probably best to measure that yourself whether it has any impact
fascinating-indigo
fascinating-indigoOP8mo ago
@Manuel Schiller What I meant is: is there a difference between having two route pages with the same layout vs having that layout be in a layout route? Is the layout component gonna re-render in the former case vs not re-render in the latter case?
foreign-sapphire
foreign-sapphire8mo ago
rerender when what happens?
fascinating-indigo
fascinating-indigoOP8mo ago
When you navigate from one page to the other
foreign-sapphire
foreign-sapphire8mo ago
depends on your structure. can you share a concrete scenario?
fascinating-indigo
fascinating-indigoOP8mo ago
Yeah sure, give me a sec Case 1 routes/app.tsx
export const Route = createFileRoute("/app")({
component: Layout,
});

function Layout() {
return (
<LayoutComponent>
<Outlet />
</LayoutComponent>
);
}
export const Route = createFileRoute("/app")({
component: Layout,
});

function Layout() {
return (
<LayoutComponent>
<Outlet />
</LayoutComponent>
);
}
routes/app/a.tsx
export const Route = createFileRoute("/app/a")({
component: Page,
});

function Page() {
return <PageA />
}
export const Route = createFileRoute("/app/a")({
component: Page,
});

function Page() {
return <PageA />
}
routes/app/b.tsx
export const Route = createFileRoute("/app/b")({
component: Page,
});

function Page() {
return <PageB />
}
export const Route = createFileRoute("/app/b")({
component: Page,
});

function Page() {
return <PageB />
}
Case 2 routes/app/a.tsx
export const Route = createFileRoute("/app/a")({
component: Page,
});

function Page() {
return (
<LayoutComponent>
<PageA />
</LayoutComponent>
);
}
export const Route = createFileRoute("/app/a")({
component: Page,
});

function Page() {
return (
<LayoutComponent>
<PageA />
</LayoutComponent>
);
}
routes/app/b.tsx
export const Route = createFileRoute("/app/b")({
component: Page,
});

function Page() {
return (
<LayoutComponent>
<PageB />
</LayoutComponent>
);
}
export const Route = createFileRoute("/app/b")({
component: Page,
});

function Page() {
return (
<LayoutComponent>
<PageB />
</LayoutComponent>
);
}
@Manuel Schiller I guess the question is, what is the difference between these two cases and why should I prefer case 1 over case 2?
foreign-sapphire
foreign-sapphire8mo ago
when navigating from /app/a to /app/b? then /app would not re-render in case 1 (unless it subscribed to some router state that changed due to the navigation)
fascinating-indigo
fascinating-indigoOP8mo ago
And in case 2 it would re-render?
foreign-sapphire
foreign-sapphire8mo ago
the LayoutComponent?
fascinating-indigo
fascinating-indigoOP8mo ago
yes
foreign-sapphire
foreign-sapphire8mo ago
maybe! it depends if react can reconcile this or not
fascinating-indigo
fascinating-indigoOP8mo ago
So I'm guessing that in case 2 if I had in both pages this:
return (
<LayoutComponent key="layout">
{...}
</LayoutComponent>
);
return (
<LayoutComponent key="layout">
{...}
</LayoutComponent>
);
then it wouldn't re-render
foreign-sapphire
foreign-sapphire8mo ago
maybe really depends on react and the specific circumstances
fascinating-indigo
fascinating-indigoOP8mo ago
I see
foreign-sapphire
foreign-sapphire8mo ago
i would definitely go for option 1
fascinating-indigo
fascinating-indigoOP8mo ago
Ok thanks 👍

Did you find this page helpful?