nextRound
TechnologiesCoding ProblemsBookmarksLearning Paths
nextRound
TechnologiesCoding ProblemsBookmarksLearning Paths
nextRound

AI-powered interview preparation platform. Practice with curated questions, mock interviews, and personalized learning paths to crack your dream tech interview.

Quick Links

  • Technologies
  • Saved Questions
  • Pricing

Company

  • About Us
  • Contact Us

Legal

  • Privacy Policy
  • Terms of Use

© 2026 nextRound. All rights reserved.

TOPIC OVERVIEW

nextjs/Middleware

Code that runs before a request reaches its route, on every matching request.

10 QuestionsAdvancedHigh Priority ~11 min read

A single middleware.ts file at the project root runs before a request completes routing, for every request that matches its configured matcher. It's the right place for logic that needs to happen before a page even starts rendering — redirects, rewrites, authentication gating, A/B test bucketing, or adding response headers uniformly across many routes.

Middleware runs exclusively on the Edge runtime, which means it inherits the same restricted API surface as any Edge function — no direct database connections through Node-specific drivers, no filesystem access. Because it sits on the hot path of every matching request, it needs to stay fast and lightweight; heavier logic, like a full auth check against a database, usually belongs in the actual route or a Route Handler, with Middleware handling only the cheap, request-level decision.

What you'll walk away knowing

What kinds of tasks belong in Middleware — redirects, rewrites, auth gating, header manipulation
Why Middleware runs exclusively on the Edge runtime, and what that restricts it from doing
How the matcher config controls which requests actually run through Middleware
Why heavy logic should stay out of Middleware even though it technically could run there
1What is Next.js Middleware? Where does it run, and what can it do that API routes cannot?Intermediate2What is the difference between middleware rewrite and redirect? How does each affect the browser URL?Intermediate3How does middleware interact with static pages? Does it run for SSG pages?Intermediate4How would you implement geo-based routing (serve different content by country) using middleware?Intermediate5What are the limitations of Middleware in Next.js? What can't you do in middleware?Intermediate6How would you implement A/B testing using Next.js middleware and routing?Intermediate7How do you write middleware that applies to some routes but not others? Explain the matcher config.Intermediate8How do you implement role-based route protection — what's the difference between doing it in middleware vs layout vs page?Intermediate9How would you implement a feature flag system that routes users to different page variants using middleware?Intermediate10Describe middleware.js.Intermediate
All Topics
1
Basics
Project Structure
Runtime
Compiler
2
SSG
SSR
CSR
3
Routing
Route Handlers
Route Groups
Dynamic Routes
Parallel Routes
Intercepting Routes
4
Middleware
Server Actions
APIS
Data Fetching
Caching
Clients
5
Optimisation
i18n
Security
Architecture
Deployment
Accessibility
Sharethis topic

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.