In a plain React app you usually write a list of routes by hand: this path shows that component. In Next.js you do not write that list. The folders in your project are the list. Think of the folders as the streets of a town and the page files as the houses. To add a new house you build it on the right street, and the town map updates itself. This idea is called file-based routing.
The two rules
- A folder inside the app folder adds one part to the URL. A folder called about gives the path /about.
- A folder is only reachable if it holds a file called page. Without that file the folder is just a folder, even if it has other files in it.
The root of your site is app/page.tsx, which answers the address /. Here is a small about page that lives in app/about/page.tsx.
import Button from "@/components/Button";
import ArticleCard from "@/components/ArticleCard";
export default function AboutPage() {
return (
<>
<h1>About us</h1>
<ArticleCard />
<Button>Contact us</Button>
</>
);
}
That is all it takes. Save the file, open /about and the page is there. The file must have a default export, which is the component that shows the page. The name of the function does not matter. The name of the file does, so it must be page followed by .tsx, .jsx, .ts or .js.
Nested routes
To make a path with two or more parts, put folders inside folders. A page at app/blog/page.tsx answers /blog. A page at app/dashboard/settings/page.tsx answers /dashboard/settings. You can go as deep as you like, but real sites stay shallow so that URLs stay short and readable.
app/
page.tsx -> /
about/page.tsx -> /about
blog/page.tsx -> /blog
dashboard/
page.tsx -> /dashboard
settings/page.tsx -> /dashboard/settings
Notice that dashboard has its own page.tsx and also holds a settings folder. A folder can be a page and a parent of other pages at the same time.
A list page with links
Here is a blog list page. It shows two posts and links to each. The Link component comes from next/link and works like an anchor tag, but it moves between pages without reloading the whole site. You will learn it fully in a later lesson.
import Link from "next/link";
const posts = [
{ slug: "hello-nextjs", title: "Hello Next.js" },
{ slug: "routing-basics", title: "Routing basics" },
];
export default function BlogPage() {
return (
<ul>
{posts.map((p) => (
<li key={p.slug}>
<Link href={`/blog/${p.slug}`}>{p.title}</Link>
</li>
))}
</ul>
);
}
The list uses the key prop, like any React list. Each link points to /blog/ followed by the post slug. A slug is the short readable part of a URL, such as hello-nextjs. We will build the page for a single post in the next lesson.
Special files in a route folder
page is one of several file names that Next.js understands. Each one has a job. You will meet them all in this course.
- page: the content of the route, and what makes it reachable.
- layout: a wrapper that stays on screen when you move between pages inside the folder.
- template: like a layout, but it is created again on each visit.
- loading: a quick screen shown while the page waits for data.
- error: a screen shown when something in the route breaks.
- not-found: a screen for pages that do not exist.
- route: a server endpoint instead of a page. A folder can hold page or route for the same URL, but not both.
Other files can sit next to page
Because only page makes a folder public, you can safely keep other files next to it, such as a component used only by that route, a style file or a test. They are not turned into URLs. This is called colocation. It keeps related files together, so you do not have to search the whole project. If you want to be extra clear that a folder is not a route, you can name it with an underscore, which you will see in the lesson about folder organisation.
Check what you built
When you run npm run build, Next.js prints every route it found. Compare it with your folders. A page that is missing from the list is usually a file with the wrong name, for example Page.tsx with a capital letter or a page without a default export.
Route (app)
┌ ○ /
├ ○ /_not-found
├ ○ /about
├ ○ /blog
├ /blog/[slug]
│ ├ ◐ /blog/[slug]
│ ├ ○ /blog/hello-nextjs
│ └ ○ /blog/routing-basics
├ /docs/[[...slug]]
│ └ ◐ /docs/[[...slug]]
├ ○ /old-page
├ ○ /pricing
└ /shop/[...slug]
└ ◐ /shop/[...slug]
○ (Static) prerendered as static content
◐ (Partial Prerender) prerendered as static HTML with dynamic server-streamed content
Your task
- Create pages for /about, /contact and /services with a different heading on each.
- Create /dashboard and /dashboard/settings. Visit both.
- Add a folder called team without a page file and visit /team. Note what you see, then add a page file and visit again.
- Run npm run build and match every folder you made to a line in the route list.
How to check it worked
- Each folder with a page file opens at the matching address.
- A folder with no page file shows the not-found page.
- You can name three special files besides page.