Performance: Nested routes and layouts
Nested routes and layouts impact initial page load speed and rendering performance by controlling how much UI is reused and how many components are rendered or re-rendered.
import { Outlet } from "@remix-run/react"; export default function RootLayout() { return ( <html> <body> <Header /> <Outlet /> <Footer /> </body> </html> ); } // Nested route file: routes/dashboard.jsx export default function Dashboard() { return <main>Dashboard content</main>; }
export default function App() {
return (
<div>
<Header />
{window.location.pathname === '/dashboard' && <Dashboard />}
{window.location.pathname === '/settings' && <Settings />}
<Footer />
</div>
);
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual conditional rendering of pages inside a single component | High - rebuilds many nodes | Multiple reflows per route change | High paint cost due to full layout repaint | [X] Bad |
| Nested routes with shared layouts and <Outlet> | Low - reuses layout nodes | Single reflow limited to changed content | Lower paint cost by partial updates | [OK] Good |
<Outlet /> to render child routes inside a parent layout.<Outlet /> is the correct built-in component for this purpose./routes
├── layout.tsx
└── dashboard.tsx
└── dashboard/
└── settings.tsx
If layout.tsx contains a <Outlet />, what will happen when the user visits /dashboard/settings?dashboard.tsx matches /dashboard as parent, dashboard/settings.tsx matches /dashboard/settings as child.layout.tsx (root layout) with <Outlet /> wraps all routes. dashboard.tsx renders inside layout's Outlet, containing settings via its own Outlet.export default function Layout() {
return (
<div>
<header>Header</header>
<main>Content</main>
</div>
);
}
Why does the nested route content not appear inside this layout when visiting a nested page?<Outlet /> to render nested routes inside the layout.<Outlet /><Outlet /> is present.<Outlet /><Outlet />, nested route content has no place to render, so it does not appear./routes
└── blog.tsx
└── blog/
├── layout.tsx
└── post.tsx
Which setup correctly shows the sidebar on all blog pages including individual posts?blog/layout.tsx is the nested layout wrapping all blog child routes including post.tsx.<Outlet /> in nested layout<Outlet /> in blog/layout.tsx to render nested content alongside sidebar. blog.tsx is the parent route for /blog but does not wrap nested routes inside the folder.Pass the test and this pattern moves unknown