PortfolioHire me for one like this

Sample website by Chayangkoon

CodeSprout

Lesson 3 / 6

Lesson 3

Pages and routes

8 min read

In a plain HTML site, every page is a file: about.html becomes /about.html. Next.js keeps that simple idea but uses folders. Everything lives inside the app folder, and each folder becomes part of the URL. A route is simply a URL your site answers to.

A folder is a URL

A folder only becomes a page when it contains a file called page.tsx (or page.js). That file exports a React component, which is a function that returns something that looks like HTML.

app/
├── page.tsx          →  /
├── about/
│   └── page.tsx      →  /about
└── blog/
    ├── page.tsx      →  /blog
    └── [slug]/
        └── page.tsx  →  /blog/my-first-post

Folders without a page.tsx are not pages, so you can keep helper files next to your routes without accidentally publishing them. A folder name in square brackets, like [slug], is a dynamic segment: one file handles many URLs, and the part of the URL is passed to your page as a value you can read.

app/about/page.tsx
export default function AboutPage() {
  return (
    <main>
      <h1>About us</h1>
      <p>We teach beginners to build for the web.</p>
    </main>
  );
}

If you know HTML, this should feel familiar. The markup inside return ( ... ) is called JSX. It is almost HTML, with a few changes: you write className instead of class, and every tag must be closed.

Layouts: the parts that stay the same

Most sites repeat the same header and footer on every page. Instead of copying them, you put them in a layout.tsx file. A layout wraps every page in its folder and in all the folders inside it. The root layout in app/layout.tsx is required, and it is where the <html> and <body> tags live.

Linking between pages

You can still use a normal <a> tag, but Next.js gives you a <Link> component that is faster. It loads the next page in the background, then swaps in only the parts that changed, without a full page reload.

That is the whole routing system: folders for URLs, page.tsx for the content, layout.tsx for shared parts, and <Link> to move between them. Next, you will break pages into smaller reusable pieces called components.