Explain it simply
Explain it simply
A new project shows a lot of files, but you do not need to memorise them all at once. Understanding the three places you touch every day is enough to start: app/ for your pages, public/ for images and icons, and package.json for dependencies and scripts.
Think of the app folder as a site map. Each folder is one part of a URL, and only a file named exactly page.tsx makes that URL open in the browser. Files with any other name, however many there are, never become URLs.
Problem → Why → Solution
The problem: expecting your own file name to create a route
A developer creates app/about/about-page.tsx for an About page, opens /about and gets a 404. They also put the logo in public/logo.png and write src="/public/logo.png", and the image is broken. There is no typo in the code; two Next.js file conventions were simply unknown.
app/
|-- page.tsx -> / (works)
`-- about/
`-- about-page.tsx -> 404: not named page.tsx
public/
`-- logo.png <img src="/public/logo.png"> -> broken imageWhy does this happen?
Inside app/, Next.js treats only reserved names such as page.tsx and layout.tsx as special files and sees everything else as ordinary modules. Files in public/ are served from the site root, so the word "public" is not part of the URL: the logo lives at /logo.png.
Core concept: folder responsibilities and special files
| Location | Responsibility |
|---|---|
| app/ | Routes, layouts and UI: the App Router lives here |
| public/ | Static files such as images, icons and robots.txt, served from the root (/) |
| package.json | Dependencies and the dev/build/start scripts |
| next.config.ts | Framework settings (remote image domains, redirects and so on) |
| tsconfig.json | TypeScript settings and import aliases such as @/* |
| .next/ | Build output generated by Next.js; never committed |
Inside app/, exact file names carry meaning. page.tsx makes a route public and layout.tsx wraps it in shared UI. loading.tsx, error.tsx and not-found.tsx define loading, error and 404 UI, while route.ts creates an API endpoint instead of an HTML page. A folder cannot hold both page.tsx and route.ts, because it must be clear which one answers the URL.
Files without a special name can live inside app/ too; this is called colocation. app/notes/note-card.tsx sits next to the /notes page but never becomes a URL. Starting a folder name with an underscore, such as _lib, takes that folder and everything below it out of routing, so even a page.tsx inside it is not a route.
Components shared by many pages commonly go in components/, and data functions and utilities in lib/. Those folder names are a team convention, not a Next.js rule. Some projects also put app/ inside a src/ folder; the routing rules stay the same.
Some root files are not yours to edit. Next.js generates next-env.d.ts for its TypeScript types, and .next/ is recreated on every build. next.config.ts is what you edit to change framework behaviour, for example to allow images from a remote server or to add redirects, and the dev server needs a restart after you change it.
- Is the file under app/?
- Is any folder prefixed with _?
- Is the file named page.tsx?
- The folder path becomes the URL
If any check fails, the file is not a route, just an ordinary module you import.
General pattern: route files and non-route files
app/<segment>/page.tsx -> /<segment>
app/<segment>/layout.tsx -> wraps /<segment> and below
app/<segment>/route.ts -> API endpoint at /<segment>
app/<segment>/any-name.tsx -> not a route (colocated)
app/_private/page.tsx -> not a route (private folder)
public/<file> -> /<file>Special file names create routes; any other name is just a module stored alongside. Folders starting with _ leave routing entirely, and files in public/ get a URL without "public" in it.
Example: the Myanmar Notes folder structure
In the tree below, each file is labelled with the URL it creates or the job it does. Separate the files that are routes from the ones that are ordinary modules.
This is the early shape of Myanmar Notes. Later lessons add loading.tsx, error.tsx and route.ts files for an API, but the rules stay the same. Before reading the labels, guess how many routes the tree contains, then compare your guess with the result below.
myanmar-notes/
|-- app/
| |-- layout.tsx root layout: wraps every page
| |-- page.tsx -> /
| |-- globals.css
| `-- notes/
| |-- page.tsx -> /notes
| |-- note-card.tsx colocated component, not a route
| `-- [id]/
| `-- page.tsx -> /notes/[id], e.g. /notes/routing
|-- components/ shared UI, never a route
|-- lib/
| `-- data.ts data access helper
|-- public/
| `-- logo.png served as /logo.png
|-- next.config.ts
|-- package.json
`-- tsconfig.jsonCode breakdown
- Root files
| |-- layout.tsx root layout: wraps every pageapp/layout.tsx is the root layout for the whole app and app/page.tsx is "/". The next lesson writes the root layout in detail.
- notes/
| |-- page.tsx -> /notesThe notes folder is the /notes segment, and the page.tsx inside it makes that URL reachable.
- Colocated file
note-card.tsxIt sits beside the page but has no special name, so there is no /notes/note-card URL. It exists only for the notes page to import.
- Dynamic segment
[id]/A folder in square brackets is a dynamic segment: one page answers /notes/routing, /notes/42 and every similar URL. The dynamic routes lesson shows how to read the id.
- Outside app/
|-- components/ shared UI, never a routecomponents/ and lib/ sit outside app/, so nothing in them becomes a URL. public/logo.png, on the other hand, is served as /logo.png.
You get three routes: /, /notes, and /notes/[id] for each note (for example /notes/routing). note-card.tsx, components/ and lib/data.ts are not routes, and the logo is available at /logo.png.Import aliases: importing with @/
As folders nest deeper, relative paths like ../../../lib/data become hard to read and break whenever a file moves. create-next-app adds an @/* alias to tsconfig.json, so you can write @/lib/data starting from the project root.
// tsconfig.json has: "paths": { "@/*": ["./*"] }
import { getNote } from "@/lib/data"; // instead of "../../../lib/data"
import { Header } from "@/components/header";Both TypeScript and the Next.js bundler understand the alias, so editor autocomplete and "Go to definition" work too. In a project that uses a src/ folder the alias points at ./src/*, so @/lib/data means src/lib/data.
In real projects
Structure in real projects
As projects grow, teams mostly use one of two styles. The first keeps all non-route code outside app/ (in components/ and lib/) and uses app/ purely for routing. The second colocates a feature's components and helpers inside its route folder, in a private folder such as _components.
Next.js does not force either style; what matters is that the whole team follows one. A new developer should understand the site's URL map just by looking at app/. Myanmar Notes keeps shared UI in components/ and data access in lib/, and colocates small components used by a single page next to that route. When a colocated component later becomes useful on a second page, it moves up to components/.
Common mistakes
Common mistakes
1. Using your own file name instead of page.tsx
app/about/about-page.tsx or app/about/index.tsx does not create /about; you get a 404. Name each route's entry file exactly page.tsx and default-export its component.
2. Referencing public/ files as /public/...
The URL of public/logo.png is /logo.png; writing src="/public/logo.png" gives a broken image. Likewise public/images/hero.jpg is served at /images/hero.jpg.
3. Putting page.tsx and route.ts in the same folder
With both app/notes/page.tsx and app/notes/route.ts, two files claim the same URL and Next.js reports a conflict. Put the API in its own folder, such as app/api/notes/route.ts.
4. Editing .next/ or next-env.d.ts by hand
Next.js generates these files, so any edit is overwritten on the next build or dev run. To add types of your own, create a separate .d.ts file, for example in a types/ folder.
Best practices
Best practices
- Name folders after the words you want in the URL (lowercase, with dashes) so app/ reads like the site's URL map.
- Keep a component used by one page next to that route, and components used by many pages in components/.
- When a folder inside app/ must never become a route, mark it clearly with the _ prefix.
- Use the @/ alias instead of long relative paths.
Mini challenge
Mini challenge: a file-path-to-URL function
Turn this lesson's routing rules into code. routeFor(filePath) returns a file's URL, or null when the file is not a route.
- Only page.tsx (or page.ts/.jsx/.js) under app/ counts as a route
- app/page.tsx must map to "/"
- Return null when any folder starts with _
- For files in public/, return the URL without "public" (public/logo.png -> /logo.png)
- Print the result for eight sample files
const PAGE_FILE = /^page\.(tsx|ts|jsx|js)$/;
function routeFor(filePath: string): string | null {
// TODO: split the path into parts
// TODO: public/ files -> "/" + the rest of the path
// TODO: anything outside app/ -> null
// TODO: last part must match PAGE_FILE
// TODO: a folder starting with "_" -> null
// TODO: join the folders between app/ and the file into the URL
}
const files: string[] = [
// TODO: eight paths: app/page.tsx, a nested page, a dynamic page,
// a colocated component, a private folder, components/, public/
];
// TODO: print "path -> url" or "path -> not a route" for each fileExpected: app/page.tsx -> / app/notes/page.tsx -> /notes app/notes/[id]/page.tsx -> /notes/[id] app/notes/note-card.tsx -> not a route app/_drafts/page.tsx -> not a route components/header.tsx -> not a route public/logo.png -> /logo.png app/about/about-page.tsx -> not a route
Show hint
Split with filePath.split("/") and remove the file name with pop(). parts.slice(1) is then the folders after app/; for an empty array, "/" + [].join("/") is "/".
Show solution
const PAGE_FILE = /^page\.(tsx|ts|jsx|js)$/;
function routeFor(filePath: string): string | null {
const parts = filePath.split("/");
if (parts[0] === "public") return "/" + parts.slice(1).join("/");
if (parts[0] !== "app") return null;
const fileName = parts.pop() ?? "";
if (!PAGE_FILE.test(fileName)) return null;
const folders = parts.slice(1);
if (folders.some((folder) => folder.startsWith("_"))) return null;
return "/" + folders.join("/");
}
const files: string[] = [
"app/page.tsx",
"app/notes/page.tsx",
"app/notes/[id]/page.tsx",
"app/notes/note-card.tsx",
"app/_drafts/page.tsx",
"components/header.tsx",
"public/logo.png",
"app/about/about-page.tsx",
];
for (const file of files) {
console.log(`${file} -> ${routeFor(file) ?? "not a route"}`);
}Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- app/ holds routes and UI, public/ static files, package.json dependencies and scripts, next.config.ts framework settings.
- Inside app/, only special names such as page.tsx, layout.tsx and route.ts mean something; other files are not routes.
- Folders starting with _ leave routing, and files in public/ are referenced from / without "public".
- components/ and lib/ are just conventions, and the @/* alias lets you import from the project root.
Next.js — Project Structure — Next.js
Next.js — Project Structure — Next.js