
Next.js 13부터 도입된 App Router에서는 기존 Page Router와 달리
애플리케이션의 UI를 페이지가 아닌 Layout 중심으로 설계한다.
이 글에서는 다음 내용을 정리한다.
layout.tsxtemplate.tsxlayout.tsx는 공통 UI를 정의하는 파일이다.
해당 경로 이하의 모든 페이지에서 공통으로 유지되는 UI를 담당한다.
예를 들어:
같은 요소들을 포함할 수 있다.
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html lang="ko">
<body>
<header>Header</header>
{children}
<footer>Footer</footer>
</body>
</html>
);
}
Next.js App Router에서는 layout이 React Tree에서 유지되기 때문이다.
즉,
👉 이 구조 덕분에:
template.tsx는 layout.tsx와 구조는 비슷하지만
페이지 이동 시마다 새로 렌더링되는 레이아웃이다.
layout은 유지되기 때문에 다음과 같은 문제가 있다:
👉 그래서 등장한 것이 template.tsx
| 구분 | layout.tsx | template.tsx |
|---|---|---|
| 렌더링 | 유지됨 | 매번 새로 렌더링 |
| 상태 유지 | O | X |
| 사용 목적 | 공통 UI | 애니메이션, 초기화 |
App Router는 children을 통해 계층 구조를 구성한다.
layout
→ template
→ page
또는 Nested 구조에서는:
Root Layout
└ Dashboard Layout
└ Template
└ Page
Next.js는 폴더 구조를 기반으로
레이아웃을 계층적으로 중첩(Nested)해서 적용할 수 있다.
app/
├─ layout.tsx
├─ page.tsx
├─ dashboard/
│ ├─ layout.tsx
│ └─ page.tsx
app/layout.tsx → 전체 공통 레이아웃dashboard/layout.tsx → dashboard 전용 레이아웃👉 /dashboard 접근 시:
Root Layout
└ Dashboard Layout
└ Page
예: /home → /dashboard
1. 기존 layout 유지
2. 새로운 page만 교체
3. template이 있다면 새로 렌더링
👉 이 구조가 Next.js 성능의 핵심
_app.tsx에서 공통 UI 관리layout.tsx를 활용하여// app/layout.tsx
<header>Global Header</header>
// app/dashboard/layout.tsx
<aside>Dashboard Sidebar</aside>
layout.tsx → 공통 UI, 상태 유지, 재사용template.tsx → 매번 새로 렌더링, 초기화 목적👉 App Router의 핵심은
페이지가 아니라 Layout 중심으로 구조를 설계하는 것