Pages And Layouts
Pages and Layouts
Pages are your route components under web/pages/. Layouts wrap pages with shared UI from web/layouts/. This guide explains how they compose and how metadata works.
Pages
A page is a React component exported as default from web/pages/:
// web/pages/about.tsx
export default function About() {
return (
<div>
<h1>About Us</h1>
<p>This is the about page.</p>
</div>
)
}Page Metadata & Rendering Strategy
Export metadata from a page to set <head> metadata (applied dynamically on client navigation and injected into static HTML during SSG prerendering) and optionally override the page's rendering strategy:
import type { Metadata } from '@dex/router'
export const metadata: Metadata = {
title: 'About Us - My Site',
description: 'Learn more about our company.',
ogTitle: 'About Us',
ogDescription: 'Learn more about our company.',
ogImage: 'https://example.com/og-about.png',
ogType: 'website',
twitterCard: 'summary_large_image',
renderStrategy: 'ssg',
}Supported Metadata fields:
title,descriptionogTitle,ogDescription,ogImage,ogVideo,ogType,urltwitterCard('summary' | 'summary_large_image' | 'player')renderStrategy('spa' | 'ssg' | 'ssr' | 'ppr' | 'dynamic')
Layouts
Layouts wrap pages with shared UI (navigation, sidebars, footers).
Global Layout
Create web/layouts/global.tsx to wrap all pages across your application:
// web/layouts/global.tsx
import { Link } from '@dex/router/client'
export default function GlobalLayout({ children }: { children: React.ReactNode }) {
return (
<div className="app-shell">
<header>
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
</nav>
</header>
<main>{children}</main>
<footer>© 2026 My Site</footer>
</div>
)
}The global layout is passed directly to <FileRouter GlobalLayout={GlobalLayout} /> in core/bootstrap/web.tsx.
Named Layouts
Create reusable named layouts in web/layouts/:
web/layouts/
├─ global.tsx # Passed to <FileRouter GlobalLayout={GlobalLayout} />
├─ dashboard.tsx # Named layout: 'dashboard'
└─ auth.tsx # Named layout: 'auth'Define the layout component (e.g. web/layouts/dashboard.tsx):
// web/layouts/dashboard.tsx
export default function DashboardLayout({ children }: { children: React.ReactNode }) {
return (
<div className="dashboard-layout flex">
<aside className="w-64 border-r">Sidebar</aside>
<section className="flex-1 p-6">{children}</section>
</div>
)
}The router generator discovers .ts and .tsx modules in web/layouts/ (including folder/index.tsx mapped to folder) and writes lazy loaders to core/router/.generated/layouts.ts.
Applying a Layout to a Page
To apply a named layout to a page, export layout as a string (or a function returning a layout name):
// web/pages/dashboard.tsx
export const metadata = { title: 'Dashboard' }
// Selects web/layouts/dashboard.tsx
export const layout = 'dashboard'
export default function DashboardPage() {
return <h1>Welcome to Dashboard</h1>
}Layout Hierarchy
When a page declares a layout, Dex composes them inside <RouterErrorBoundary> in order:
- GlobalLayout (from
web/layouts/global.tsx) - Page-declared Layout (e.g.
web/layouts/dashboard.tsx) - Page Component (from
web/pages/dashboard.tsx)
Rendered output:
<div class="app-shell">
<div class="dashboard-layout">
<aside>Sidebar</aside>
<section>
<h1>Welcome to Dashboard</h1>
</section>
</div>
</div>Passing Context with <Outlet>
Use <Outlet context={...} /> and useOutletContext() from @dex/router/client when sharing state from a layout or parent wrapper down to nested content.
Client-Only Components & Error Boundaries
<ClientOnly fallback={...}>/clientOnly(Component): Safely render browser-only UI on SSG-prerendered pages without hydration mismatches.RouterErrorBoundary: Automatically wraps rendered views and layouts and resets on route changes; pass a customerrorBoundarycomponent to<FileRouter />to customize error UI.
Best Practices
- Keep
GlobalLayoutminimal — reserve it for app-wide shells and navigation - Use named layouts — group section-specific UI under
web/layouts/ - Use
<Link>for internal navigation — enables intent prefetching and zero-flicker transitions - Define
metadataon pages — ensures accurate<head>tags during SSG and client transitions
See Also
- File-Based Routing — How pages map to routes
- Configuration — Customize layout folder and rendering strategy
- How Dex Works — Build and prerender overview