The file-system router

Beginner · 6 min

How folders become URL segments in the App Router, why a page or route file makes a segment public, and what Next.js does with a URL that matches nothing.

Before thisThe project structure

Codevolution · 6 min

The file-system router

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by CodevolutionReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Each folder inside `app` is a URL segment, and nesting folders nests the segments, so `app/blog/authors/page.tsx` serves `/blog/authors`.
  • A segment is only reachable when it contains a `page` file or a `route` file; a folder with neither is just a place to put code.
  • The file name decides what a file does and the folder path decides which URL it applies to, so no route table exists to keep in sync.
  • A URL that matches no `page` or `route` is answered by the not-found UI, and you can customize it with a `not-found` file.

The idea: URLs are folders

Most web frameworks make you write down your routes: a file or function that says “this path goes to that handler”. That list drifts. Someone renames a page and forgets the list, or deletes a screen and the route lingers.

The App Router removes the list by making the folder tree the route table. Each folder inside app is one segment of a URL path. Nest folders and you nest segments:

app/
  page.tsx                  →  /
  about/
    page.tsx                →  /about
  blog/
    page.tsx                →  /blog
    authors/
      page.tsx              →  /blog/authors

The file name carries meaning too. page.tsx says “this segment has UI you can visit.” The folder path says which URL. Between them, you know what a file does and where it applies without opening anything.

Why a page file is required

A folder alone does nothing. app/blog/ without a page.tsx is not /blog; visiting it gives a 404. This is deliberate, because it lets you keep related code beside a route without exposing it. These are all safe:

app/blog/
  page.tsx
  post-card.tsx
  format-date.ts

Only page.tsx is public, and only what it returns goes to the browser. The sibling files are modules that page.tsx can import.

A page file default-exports a React component:

// app/about/page.tsx
export default function Page() {
  return <h1>About</h1>;
}

The other file that makes a segment public is route, for endpoints that return data instead of UI. It is covered under route handlers. A segment cannot have both a page and a route file.

Other special files at the same level

page is one of several special names. A segment folder can also hold a layout (pages and layouts), a loading file (loading UI and streaming), an error file and a not-found file (error handling and not found). Each applies to the segment it sits in and everything below.

What happens for an unknown URL

Request /nope and no folder matches. Next.js renders its not-found UI with a 404 status. In a production build you can see this as the /_not-found entry in the route table next build prints. To replace the default page, add app/not-found.tsx.

Dynamic and grouped segments

Folder names can do more than spell a path. [slug] makes a segment a parameter (dynamic routes and params), (marketing) makes a folder invisible in the URL (route groups and private folders), and @modal creates a named slot (parallel and intercepting routes). The shape stays the same: structure on disk is structure in the URL, plus a few reserved characters that change the meaning.

What the video covers

Codevolution’s video (chapters: introduction, setting up a new project, file-based routing, creating a first route, adding more routes, handling non-existing routes) builds a small project and adds routes folder by folder. It uses Next.js 15, but this part of routing did not change in 16. Its follow-up on nested routes is linked as a deeper pick.

Where this fits

You know how a URL maps to a folder. Next, pages and layouts covers the UI that goes inside those folders and how the surrounding chrome is shared.

Check yourself

  1. 01Which URL does `app/shop/cart/page.tsx` serve, and which does `app/shop/cart/summary.tsx` serve?

    The page serves `/shop/cart`. `summary.tsx` serves nothing; only `page` and `route` files make a segment public, so `summary.tsx` is an ordinary module that other files can import.

  2. 02What is the benefit of deriving routes from the folder structure instead of a configuration file?

    The URL space and the code layout cannot drift apart. To see what routes exist you read the folder tree, and removing a folder removes the route, so there is no separate list to update.

  3. 03What does Next.js show when you visit a URL that has no matching route?

    It renders the not-found UI with a 404 status. A built-in default is used unless you add your own `not-found` file.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark