Skip to the solution
EasyFrontend6 min read

How does file-based routing work in the Next.js App Router?

922 views

The problem

Question presented to candidate: "Walk me through how a URL like /blog/my-post maps to files in the app directory. What happens to a helper file or a components folder that sits next to page.tsx?"

What a strong answer should cover:

  • Folders define URL segments; a route is public only when the folder contains a page file (or a route file for an API endpoint).
  • Other files in the folder (components, utils, styles) are colocated safely and never become routes.
  • Dynamic segments use brackets: [slug], catch-all [...slug], optional catch-all [[...slug]].
  • Route groups (name) organise folders without adding a URL segment; private folders _name opt out of routing entirely.
  • Special files in a segment (layout, loading, error, not-found) add UI around the page for that segment and below.

Clarifying questions expected:

  • "Is this App Router or Pages Router?" β€” in the Pages Router every file under pages/ is a route.
  • "Should shared layouts apply to a subset of pages?" β€” that is what route groups are for.

Code / implementation expected: Yes β€” a small app tree and the URLs it produces.

  • nextjs
  • routing
  • app-router
  • file-based

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Read the code

An app tree and the URLs it produces
app/
  layout.tsx                  # root layout for every page
  page.tsx                    # /
  (marketing)/
    about/page.tsx            # /about          (group name dropped)
  blog/
    page.tsx                  # /blog
    [slug]/page.tsx           # /blog/one, /blog/two, ...
  dashboard/
    page.tsx                  # /dashboard
    util.ts                   # not a route
    _components/Widget.tsx    # not a route (private folder)
    nopage/Widget.tsx         # not a route (no page.tsx)
  api/health/route.ts         # /api/health  (GET, POST handlers)

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 6 of 129 in Next.js

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

All Next.js questions
Up nextEasy

next/font and next/image β€” how do they combine for Core Web Vitals?

Continue