Skip to content
Learn Netverks

Lesson

Step 9/36 25% through track

route-groups-middleware

Route groups and middleware

Last reviewed May 28, 2026 Content v20260528
Track mode
client_nextjs
Means
In-browser Next.js (client components)
Reading
~1 min
Level
advanced

This lesson

This lesson teaches Route groups and middleware: the concepts, APIs, and habits you need before advancing in Next.js.

Routing maps URLs to components—guards and resolvers protect enterprise flows.

You will apply Route groups and middleware in contexts like: Marketing sites, dashboards, e-commerce, and Vercel-style deployments that need hybrid static + dynamic pages.

Write TSX for Client Components, click Run—React 18 CDN + in-browser TSX compile; use client/server lessons explain App Router concepts; mountApp renders interactive UI; printOutput feeds the terminal.

When hooks, state, and effects from intermediate lessons are familiar.

Route groups organize routes without affecting URLs. Middleware runs before a request completes—ideal for auth redirects, geo headers, and A/B flags at the edge.

Route groups

app/
  (marketing)/
    about/page.tsx      → /about
    pricing/page.tsx    → /pricing
  (app)/
    dashboard/page.tsx  → /dashboard

Parentheses strip the group name from the URL—purely for file organization and shared layouts per group.

middleware.ts

project-root/
  middleware.ts   # not inside app/
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';

export function middleware(request: NextRequest) {
  if (!request.cookies.get('session')) {
    return NextResponse.redirect(new URL('/login', request.url));
  }
  return NextResponse.next();
}

export const config = { matcher: ['/dashboard/:path*'] };

When to use which

  • Route group layout — different shells for marketing vs app chrome
  • Middleware — cheap checks before render (auth, locale prefix, bot blocking)
  • Server Component — data-heavy auth that needs database lookups

Self-check

  1. Does (shop) appear in the browser URL?
  2. Why not put all auth logic only in middleware?

Interview prep

When use middleware?

Edge checks before render—auth redirects, locale, A/B flags—not heavy database business logic (use Server Components for that).

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Route group without URL?
  • Middleware runs where?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump