Middleware — Complete Guide
Middleware — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Next.js Tutorial on Toolliyo Academy.
On this page
Next.js Tutorial (LearnHub) · Lesson 21 of 100
Middleware
Beginner ✓ → Intermediate → Advanced → Professional
Intermediate · 2 — Building apps · ~14 min read · Module 3: Data & Forms
Introduction
You know the basics now. Here we use Middleware in real LearnHub screens — layouts, data, and APIs. Still plain language, just a bit more depth. middleware.ts runs before a request completes. It can redirect, rewrite URLs, or set headers based on cookies and path — ideal for auth gates and locale detection. Guests should never reach /dashboard HTML even for a millisecond. Middleware redirects to /login at the edge before the page renders.
Data and forms power course lists, enrollments, and progress. Learn these patterns slowly — test in a small page first.
When will you use this?
Reach for data fetching and Server Actions when pages need database content or form submissions.
- Enrollments, quiz scores, and course progress load from the server — often with Server Actions.
- Forms that enroll a student in a course use Server Actions instead of a separate REST call.
Real-world: LearnHub LMS
The EdTech / LMS team building LearnHub LMS uses Middleware to redirect guests away from /dashboard before the page renders. students and instructors never see the TypeScript files — they just get a fast, reliable course catalog, lesson player, and progress tracking.
Production-style code
// middleware.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const token = request.cookies.get('session')?.value;
if (!token && request.nextUrl.pathname.startsWith('/dashboard')) {
return NextResponse.redirect(new URL('/login', request.url));
}
return NextResponse.next();
}
export const config = { matcher: ['/dashboard/:path*'] };
What happens in production: In LearnHub LMS, getting Middleware right means students and instructors trust the course catalog, lesson player, and progress tracking every day.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
const session = request.cookies.get('session')?.value;
if (!session && request.nextUrl.pathname.startsWith('/dashboard')) {
return NextResponse.redirect(new URL('/login', request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ['/dashboard/:path*', '/instructor/:path*']
};
Line-by-line walkthrough
| Code | What it means |
|---|---|
import { NextResponse } from 'next/server'; | Imports a module so you can use its exports in this file. |
import type { NextRequest } from 'next/server'; | Imports a module so you can use its exports in this file. |
export function middleware(request: NextRequest) { | Named export — reusable function or component. |
const session = request.cookies.get('session')?.value; | Part of the Middleware example — read it together with the lines before and after. |
if (!session && request.nextUrl.pathname.startsWith('/dashboard')) { | Part of the Middleware example — read it together with the lines before and after. |
return NextResponse.redirect(new URL('/login', request.url)); | Next.js helpers for Route Handlers — read request and return JSON or redirects. |
} | Closes a block started by { above. |
return NextResponse.next(); | Next.js helpers for Route Handlers — read request and return JSON or redirects. |
} | Closes a block started by { above. |
export const config = { | Part of the Middleware example — read it together with the lines before and after. |
matcher: ['/dashboard/:path*', '/instructor/:path*'] | Part of the Middleware example — read it together with the lines before and after. |
}; | Closes a block started by { above. |
How it works (big picture)
- matcher limits which paths run middleware.
- redirect sends 307 to login.
- next() continues normally for signed-in users.
Do this on your computer
- Create middleware.ts at project root (or src/)
- Set a fake session cookie in browser devtools
- Visit /dashboard logged out — confirm redirect
- Add instructor path to matcher
- Read the real-world section and name which part of LearnHub uses this topic.
- Run the example locally with npm run dev and confirm the same behavior.
- Change one value in the example (route, text, or course id) and predict what will happen before you save.
Experiments — try changing this
- Change a string or route in the example and save — watch the browser update.
- Break the code on purpose (remove a bracket), read the error overlay, then fix it.
- Use npm run dev while editing Middleware — the page hot-reloads on save.
Remember
middleware.ts runs before routes. Use for auth redirects and headers. matcher controls scope.
Common questions
Middleware vs layout auth?
Middleware runs earlier; use both for defense in depth.
How long should I spend on Middleware?
Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new concept; setup lessons may take one afternoon.
What if I get stuck on Middleware?
Re-read the line-by-line walkthrough, check the terminal and browser overlay for errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.
Where is Middleware used in real jobs?
See the real-world section above — the same pattern appears in LMS, e-commerce, SaaS, and dashboards. Interviewers ask you to explain it using one concrete example.