0
0
NextJSframework~10 mins

Why middleware intercepts requests in NextJS - Visual Breakdown

Choose your learning style9 modes available
Concept Flow - Why middleware intercepts requests
Incoming Request
Middleware Intercepts
Modify Request
Pass to Next
Final Handler (Page/API)
Middleware catches requests first to check or change them before they reach the final page or API.
Execution Sample
NextJS
import { NextResponse } from 'next/server';

export function middleware(request) {
  if (!request.nextUrl.pathname.startsWith('/admin')) {
    return NextResponse.next();
  }
  return NextResponse.redirect(new URL('/login', request.url));
}
This middleware checks if the path starts with '/admin'. If not, it lets the request continue. Otherwise, it redirects to '/login'.
Execution Table
StepRequest PathConditionActionResult
1/homeDoes path start with '/admin'? NoAllow request to continueRequest passes to final handler
2/admin/dashboardDoes path start with '/admin'? YesRedirect to '/login'Response sent with redirect
3/admin/settingsDoes path start with '/admin'? YesRedirect to '/login'Response sent with redirect
4/aboutDoes path start with '/admin'? NoAllow request to continueRequest passes to final handler
💡 Middleware stops or passes request based on path check
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
request.nextUrl.pathname/home/home/admin/dashboard/admin/settings/about
condition (startsWith '/admin')N/Afalsetruetruefalse
action takenN/Anext()redirect('/login')redirect('/login')next()
Key Moments - 2 Insights
Why does middleware run before the page or API handler?
Middleware intercepts requests first to allow checks or changes before the final handler runs, as shown in the execution_table where action depends on the condition before passing on.
What happens if middleware redirects a request?
If middleware redirects, it sends a response immediately and the request does not reach the final handler, as seen in steps 2 and 3 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what action is taken when the request path is '/about'?
ARedirect to '/login'
BAllow request to continue
CBlock the request
DModify the request path
💡 Hint
Check the row where Request Path is '/about' in the execution_table
At which step does the middleware send a redirect response?
AStep 2
BStep 1
CStep 4
DStep 5
💡 Hint
Look for 'redirect' action in the execution_table rows
If the condition changed to check for '/user' instead of '/admin', what would happen at step 2?
ABlock the request
BRedirect to '/login'
CAllow request to continue
DCause an error
💡 Hint
Consider the condition and path '/admin/dashboard' at step 2 in variable_tracker
Concept Snapshot
Middleware in Next.js runs before pages or APIs.
It checks or changes requests.
It can allow, redirect, or block requests.
Use conditions on request paths.
Return NextResponse.next() to continue.
Return NextResponse.redirect() to redirect.
Full Transcript
Middleware in Next.js intercepts incoming requests before they reach the final page or API handler. It checks the request path and decides what to do. For example, if the path starts with '/admin', middleware can redirect the user to a login page. Otherwise, it lets the request continue. This lets developers control access or modify requests early. The execution table shows requests with different paths and how middleware acts on them. Variables track the path, condition result, and action taken at each step. Key moments clarify why middleware runs first and what happens on redirect. The quiz tests understanding of these steps and outcomes.