Next.js App Router Basics
The Next.js App Router is the file-based routing system that turns folders inside the app directory into URLs. It is the default router in Next.js 16 and the foundation of every project in this course. Understanding it early makes every later tutorial easier.
This tutorial teaches you how the app directory works, which special files Next.js recognises, and how to build your first routes. You will create three real pages and see exactly how each folder maps to a URL. If you have not set up a project yet, start with Next.js Setup & Project Structure first.
What Is the Next.js App Router?
The Next.js App Router is a routing system where folders inside the app directory define URL segments. A folder named about creates the /about route. A file named page.tsx inside that folder makes the route publicly accessible. No router configuration file is needed .
Here is the simplest possible example. The file path itself is the routing instruction.
app/
page.tsx → /
about/
page.tsx → /about
How it works: Next.js reads the folder structure at build time and during development. Each folder adds a segment to the URL. The page.tsx file is the component that renders when someone visits that URL. Folders without a page.tsx do not create a public route on their own.
How Do Folders Define URL Segments?
Each folder inside app becomes one part of the URL path. Nested folders create nested URL segments. This is called file-based routing, and it removes the need for a separate route configuration file .
Creating the /about Route
// app/about/page.tsx
export default function AboutPage() {
return (
<main>
<h1>About Us</h1>
<p>We build free programming tutorials for everyone.</p>
</main>
);
}
Browser shows at http://localhost:3000/about:
About Us
We build free programming tutorials for everyone.
How it works: The folder about creates the URL segment /about. The page.tsx file exports a React component as the default export. Next.js renders this component when the browser requests /about. Without the page.tsx file, the folder alone would not create a route.
Creating a Nested Route: /blog/first-post
Nested folders create nested URL paths. A folder inside blog creates a sub-path.
// app/blog/first-post/page.tsx
export default function FirstPostPage() {
return (
<main>
<h1>My First Blog Post</h1>
<p>This route is created by nested folders.</p>
</main>
);
}
Browser shows at http://localhost:3000/blog/first-post:
My First Blog Post
This route is created by nested folders.
How it works: The folder path app/blog/first-post/ maps directly to the URL path /blog/first-post. Each folder contributes one segment. You can nest as deep as you need. The page.tsx at the deepest level is the one that renders.
What Are the Special Files in the App Router?
The app directory recognises several file names that have special meaning. Each one changes how a route behaves. You do not need all of them in every folder, but you should recognise them when you see them in a project .
| File Name | Purpose |
|---|---|
page.tsx |
Makes a route publicly accessible and renders its main content. |
layout.tsx |
Wraps the page and its children with shared UI that persists across navigation. |
loading.tsx |
Shows a loading skeleton while the page or its data is loading. |
error.tsx |
Catches errors in the route segment and displays a fallback UI. |
not-found.tsx |
Renders when the route does not match or when notFound() is called. |
route.ts |
Creates an API endpoint instead of a page. Cannot coexist with page.tsx in the same folder. |
A folder can contain a page.tsx for the visible page, a layout.tsx for shared structure, and a loading.tsx for the loading state. All three work together. The Next.js Pages & Layouts tutorial covers layouts in detail.
What Does Colocation Mean in the App Router?
Colocation means you can place components, styles, and utility files inside route folders without making them public. Only page.tsx and route.ts create accessible URLs. Everything else stays private to that folder .
app/
blog/
page.tsx → /blog (public)
components/
PostCard.tsx → not public, just a component
utils.ts → not public, just a helper
How it works: Next.js ignores files and folders that are not named page.tsx or route.ts when generating routes. This lets you keep related code close together without exposing it in the URL. You can safely place a components folder next to a page.
What Is the Difference Between App Router and Pages Router?
Next.js has two routing systems. The App Router is the newer one and the default in Next.js 16. The Pages Router is the original system, still supported for older projects .
| Feature | App Router | Pages Router |
|---|---|---|
| Root folder | app/ |
pages/ |
| Route file | page.tsx |
index.tsx or any file |
| Server Components | Default | Not available |
| Layouts | Built-in via layout.tsx |
Requires custom _app.tsx |
| Loading states | Built-in loading.tsx |
Manual implementation |
| API routes | route.ts files |
pages/api/ files |
New projects should always use the App Router. The Pages Router is documented for maintenance work on existing codebases. This course teaches only the App Router.
Are Server Components the Default in the App Router?
Yes. Every component inside the app directory is a Server Component by default. Server Components run only on the server and send HTML to the browser. They can fetch data directly and keep sensitive logic away from the client .
You do not need to do anything special to use a Server Component. Just write a normal React component. When you need interactivity, like a button click or a form input, you create a separate Client Component and add "use client" at the top of that file. Details come in a later tutorial. For now, remember that the default is server-side.
Common Mistakes and Best Practices
- Creating a folder without a
page.tsxand expecting a route. Folders only define URL segments. Without apage.tsx, the route returns a 404 even though the folder exists. - Putting
page.tsxandroute.tsin the same folder. Next.js throws a build error because a route cannot be both a page and an API endpoint. Choose one per folder. - Adding
"use client"to every file. This turns every component into a Client Component and loses the benefits of server rendering. Start without it and add it only when needed. - Mixing App Router and Pages Router conventions. Creating a
pages/folder alongsideapp/causes confusion about which router handles a route. Use only the App Router in new projects. - Naming a folder with capital letters. While technically possible, lowercase folder names match URL conventions and avoid broken links on case-sensitive servers.
Real-World Example: Three Pages in One Project
This example creates the home page, an about page, and a blog post page. Together they demonstrate the core App Router pattern: folders become URLs, and page.tsx makes them visible.
app/
page.tsx → /
about/
page.tsx → /about
blog/
intro/
page.tsx → /blog/intro
// app/page.tsx
export default function HomePage() {
return (
<main>
<h1>Welcome to My Site</h1>
<p>This is the home page at the root URL.</p>
</main>
);
}
// app/about/page.tsx
export default function AboutPage() {
return (
<main>
<h1>About Me</h1>
<p>I am learning Next.js routing.</p>
</main>
);
}
// app/blog/intro/page.tsx
export default function BlogIntroPage() {
return (
<main>
<h1>Introduction to My Blog</h1>
<p>This page lives two folders deep.</p>
</main>
);
}
Browser results:
http://localhost:3000/ → Welcome to My Site
http://localhost:3000/about → About Me
http://localhost:3000/blog/intro → Introduction to My Blog
Line-by-line explanation: The first file, app/page.tsx, sits directly inside app, so it becomes the root route /. The second file lives in app/about/, so the folder name about becomes the URL segment. The third file lives in app/blog/intro/, so two segments are added: blog and intro. Each file exports a default function that returns JSX. Next.js handles the matching automatically. No imports, no router setup, no configuration file changes.
Summary
- The Next.js App Router uses folders inside
appto define URLs. A folder name becomes a URL segment. page.tsxmakes a route public. Without it, the folder exists but no URL is created.- Special files like
layout.tsx,loading.tsx, anderror.tsxchange how a route behaves. You will use them as your apps grow. - Colocation keeps components and utilities inside route folders without exposing them in the URL.
- Server Components are the default. You only add
"use client"when a component needs browser interactivity.
Now that you can create pages, continue to Next.js Pages & Layouts to learn how shared layouts wrap your pages and persist across navigation.
Frequently Asked Questions
Do I need to create a route file for every page?
Yes. Every public route needs a page.tsx file inside its folder. A folder alone does not create a route. If you create app/contact/ but forget page.tsx, visiting /contact returns a 404 error.
Can I use the Pages Router and App Router together?
Yes, Next.js supports both in the same project. Routes in app/ take priority over matching routes in pages/. However, mixing them is confusing for beginners. New projects should use only the App Router and avoid the pages/ folder entirely.
What happens if two folders create the same URL?
This cannot happen with normal folders because folder paths are unique. Conflict errors occur when you try to put both page.tsx and route.ts in the same folder, or when you use route groups that resolve to the same path. Next.js reports a clear build error in those cases.
Is the app directory required to be at the project root?
The app folder normally sits at the root of your project, next to package.json. You can optionally place it inside a src folder as src/app, which some teams prefer. If you do this, public and configuration files stay at the root .
How do I create a route that is not a page, like an API?
Use a file named route.ts instead of page.tsx. A file at app/api/hello/route.ts creates an API endpoint at /api/hello. You cannot have both page.tsx and route.ts in the same folder because a route serves either a page or an API response.
Practice Exercises
Easy: Create the /contact Route
Create a page that renders at /contact with a heading and a short message.
Show solution
// app/contact/page.tsx
export default function ContactPage() {
return (
<main>
<h1>Contact</h1>
<p>Reach us at hello@example.com.</p>
</main>
);
}
The folder name contact creates the URL segment, and page.tsx makes it public.
Easy: Create a Nested Route
Create a page at /docs/getting-started using nested folders.
Show solution
// app/docs/getting-started/page.tsx
export default function GettingStartedPage() {
return (
<main>
<h1>Getting Started</h1>
<p>Read this first.</p>
</main>
);
}
Nested folders docs and getting-started create the two URL segments.
Medium: Identify the Mistake
This folder exists: app/services/page.tsx. The developer also adds app/services/route.ts. What error will Next.js show?
Show solution
// Error: You cannot have both page.tsx and route.ts in the same folder.
// A route serves either a page or an API endpoint, not both.
A folder must choose one: a visible page or an API route. Remove one of the files to fix the conflict.
Challenge: Build Three Routes with Colocation
Create the routes /, /pricing, and /pricing/faq. Place a shared component file called PricingCard.tsx inside the pricing folder and import it in the /pricing page. Confirm that /pricing/PricingCard is not a valid URL.
Show solution
app/
page.tsx → /
pricing/
page.tsx → /pricing
PricingCard.tsx → not a route
faq/
page.tsx → /pricing/faq
// app/pricing/page.tsx
import PricingCard from "./PricingCard";
export default function PricingPage() {
return (
<main>
<h1>Pricing</h1>
<PricingCard />
</main>
);
}
// app/pricing/PricingCard.tsx
export default function PricingCard() {
return (
<div>
<p>Basic plan: ₹499 per month</p>
</div>
);
}
Only page.tsx files create routes. PricingCard.tsx is a colocated component and is not accessible at any URL.
Written by Shaikh Abdul Shahid
A Full-Stack Developer with experience in PHP, Laravel, JavaScript, and React.
Founder of Online Learner, sharing practical knowledge and programming tutorials.
Building and maintaining real-world web applications since 2017.
Your Feedback
Help us improve by sharing your thoughts.
Online Learner helps developers master programming, database concepts, interview preparation, and real-world implementation through structured learning paths.
Quick Links
© 2023 - 2026 OnlineLearner.in | All Rights Reserved.
