Три special file: layout.tsx (persist), route group (marketing) (без URL segment), template.tsx (re-mount на каждую навигацию).
Пример
// app/dashboard/layout.tsx — persists across /dashboard/* navigationexportdefaultfunctionDashboardLayout({ children }: { children: React.ReactNode }) {
return (
<divclassName="flex"><Sidebar /> {/* stays mounted, keeps scroll & open/closed state */}
<main>{children}</main></div>
);
}
// app/(marketing)/layout.tsx — route group: applies to /about, /pricing…// but "(marketing)" NEVER appears in the URL.exportdefaultfunctionMarketingLayout({ children }: { children: React.ReactNode }) {
return<section>{children}</section>;
}
// app/onboarding/template.tsx — RE-MOUNTS on every step change (fresh animation)exportdefaultfunctionTemplate({ children }: { children: React.ReactNode }) {
return<divclassName="animate-fade-in">{children}</div>;
}
Итог
Layout сохраняет state между навигациями; template сбрасывает DOM; route groups организуют файлы без влияния на URL.
Next.js: Layouts и вложенный routing — пример кода · Sobeso