Course contentsShow
← React
Lesson 1887 of 2,39643. RoutingFree lesson

File-System Routing Conventions

Understanding page.tsx, layout.tsx, loading.tsx, and error.tsx files for defining routes and UI patterns.

File-System Routing Conventions

What you'll learn: How Next.js uses special file names to automatically define routes and UI patterns without manual route configuration.

The File-System as Route Definition

Unlike React Router where you explicitly declare routes in code, Next.js App Router uses your folder structure and specific file names to automatically create routes. Think of it like organizing a filing cabinet: the drawer labels (folders) create the URL paths, and the documents inside (special files) define what happens at those paths.

The Core File Conventions

Next.js recognizes four critical filenames that each serve a specific purpose:

page.tsx — Defines the actual route. A folder only becomes a navigable URL if it contains a page.tsx. This is your route's main content.

layout.tsx — Wraps all nested routes and pages within that folder. Used for shared UI like headers or sidebars. Layouts persist across navigation, so state isn't reset.

loading.tsx — Automatically displays while page.tsx is loading its data. Next.js wraps your page in a Suspense boundary using this file.

error.tsx — Catches errors in nested routes and provides fallback UI. Must be a Client Component to handle runtime errors.

How They Work Together

In app/dashboard/layout.tsx, you define the dashboard shell. app/dashboard/page.tsx renders the dashboard home. app/dashboard/loading.tsx shows a spinner while data loads. If anything breaks, app/dashboard/error.tsx catches it.

The route tree is literal: app/products/[id]/page.tsx creates /products/:id automatically. No createRoute or Route component needed.

Key Takeaway: Next.js file-system routing uses special filenames (page, layout, loading, error) to automatically generate routes and UI patterns based on folder structure—no manual route configuration required.