← Next.js tutorial

Next.js

Pages & File-based Routing

In Next.js's App Router, a folder under app/ containing a page.js file automatically becomes a route.

Example: two routes from two files

app/page.js           -> the homepage,        "/"
app/about/page.js      -> becomes             "/about"
app/blog/[slug]/page.js -> becomes            "/blog/anything-here"
// app/about/page.js
export default function AboutPage() {
  return <h1>About us</h1>;
}

Notice the third example: square brackets in a folder name ([slug]) create a dynamic route, one file that handles any value in that position of the URL, with the actual value ("anything-here") available inside the component as a parameter. This is exactly how a blog with hundreds of posts is served from one single page file instead of one file per post.

Example
// app/about/page.js
export default function AboutPage() {
  return <h1>About us</h1>;
}
Output
Visiting /about renders an <h1> reading "About us".
Ready to go further?

These free tutorials are a preview, our Frontend Development cohort covers it in depth, with a real project and a cohort alongside you. Closes Nov 30

Enroll in Frontend Development

Cookie preferences

Choose what you're comfortable with. You can change this any time from the footer.

Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account