JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsnextjs
PrevNext

Learn the concept

File-Based Routing

nextjs
junior
routing

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

nextjs
routing
file-based
dynamic-routes
app-router
params
Quick Answer

Next.js uses a file-system based router where files and folders in the `app/` directory (recommended) or legacy `pages/` directory automatically become routes — `app/about/page.tsx` becomes `/about`.

Detailed Explanation

File-based Routing Basics:

Next.js has two routing systems:

  1. App Router (recommended) - Uses app/ directory with page.tsx files
  2. Pages Router (legacy) - Uses pages/ directory where each file is a route

App Router Conventions:

  • page.tsx - Makes a route publicly accessible
  • layout.tsx - Shared UI for a segment and its children
  • loading.tsx - Loading UI while content loads
  • error.tsx - Error UI for a segment
  • not-found.tsx - 404 UI

Route Types:

  • Static routes: app/about/page.tsx → /about
  • Dynamic routes: app/posts/[id]/page.tsx → /posts/123
  • Catch-all routes: app/docs/[...slug]/page.tsx → /docs/a/b/c
  • Optional catch-all: app/shop/[[...slug]]/page.tsx → /shop or /shop/a/b

Route Groups:

  • Folders in parentheses like (marketing) organize routes without affecting URL

Params as Promise (Next.js 16): In Next.js 16, params is a Promise that must be awaited before accessing its values. This applies to all page and layout components.

Code Examples

App Router folder structureText
app/
├── page.tsx              # / (home)
├── layout.tsx            # Root layout (wraps all pages)
├── about/
│   └── page.tsx          # /about
├── blog/
│   ├── page.tsx          # /blog (list)
│   └── [slug]/
│       └── page.tsx      # /blog/my-post (dynamic)
├── docs/
│   └── [...slug]/
│       └── page.tsx      # /docs/intro/getting-started (catch-all)
└── (marketing)/          # Route group (no URL segment)
    ├── pricing/
    │   └── page.tsx      # /pricing
    └── features/
        └── page.tsx      # /features

Real-World Applications

Use Cases

Documentation Site

Use catch-all routes (`[...slug]`) to map a docs tree from a CMS or file system into nested URL paths, with route groups for organizing marketing vs docs layouts.

Multi-Tenant SaaS

Use dynamic routes like `[orgSlug]/dashboard` to create tenant-specific pages, with middleware validating the org slug and layout components providing tenant branding.

Blog Platform

Map blog categories and posts to nested routes (`/blog/[category]/[slug]`), using `generateStaticParams` to pre-render popular posts and dynamic rendering for the rest.

Mini Projects

File-Based Wiki

beginner

Build a wiki with catch-all routes that reads markdown files from a local directory. Include a sidebar with auto-generated navigation based on the file structure, demonstrating route groups and nested layouts.

Multi-Layout Dashboard

intermediate

Create a dashboard app using route groups — `(auth)` for login/register with a minimal layout, and `(dashboard)` for authenticated pages with sidebar navigation, demonstrating how route groups share layouts without affecting URLs.

Industry Examples

Hashnode

Hashnode uses Next.js file-based routing to serve millions of blog posts under custom domains, mapping dynamic `[slug]` routes to user-generated content.

Cal.com

Cal.com leverages nested dynamic routes (`/[user]/[eventType]`) for booking pages, with parallel routes for modal-based scheduling interactions.

Resources

Next.js Routing Documentation

docs

Dynamic Routes

docs

Related Questions

How do you navigate between pages in Next.js?

junior
navigation

How do you create dynamic routes with static generation in Next.js?

mid
routing
Previous
What is Next.js and how does it differ from React?
Next
How do you navigate between pages in Next.js?
PrevNext