Core File Conventions: page.tsx, layout.tsx, template.tsx & not-found.tsx
Core File Conventions: page.tsx, layout.tsx, template.tsx & not-found.tsx
Next.js uses a special file system convention inside the app directory. Naming files with reserved conventions (page, layout, template, loading, error, not-found) automatically wires up routing, nested layouts, state persistence, and error boundaries.
1. The Core File Hierarchy
2. layout.tsx: Nested & Persistent Layouts
A layout is UI that is shared between multiple routes. Layouts preserve state, remain interactive, and do not re-render when navigating between sibling routes!
3. template.tsx vs layout.tsx
While layout.tsx preserves state across navigation, template.tsx creates a brand-new instance for each child route on navigation:
- DOM elements are recreated.
- State is not preserved.
- Effects (like
useEffect) re-sync. - Ideal for: Page entrance/exit animations (Framer Motion) or logging per-page views.
4. not-found.tsx: Custom 404 Pages
Next.js renders not-found.tsx when an invalid URL is visited, or when the notFound() function is programmatically triggered:
Multiple Choice Questions
app/ directory and must contain the <html> and <body> tags?layout.tsx and template.tsx during client-side navigation?not-found.tsx component when an entity does not exist in the database?layout.tsx component accept and render?app/dashboard/settings/) have its own nested layout.tsx?Dynamic Routes ([id]), Catch-All Routes ([...slug]) & Route Groups ((group))
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.