Next.js Middleware: Your App's Edge Bouncer
Think of Next.js Middleware as a bouncer at your app's door. It runs code on the edge before a request is processed, letting you redirect, rewrite, or block it. The main footgun is forgetting it runs in a limited Edge Runtime, not a full Node.js.
Why it exists
To centralize logic that applies to many routes without repeating code. Instead of checking for an auth token in every single protected page, you can check it once in a single middleware file that guards all those routes, keeping your page logic clean.
The mental model
Middleware is like a bouncer or a traffic cop standing in front of your entire Next.js application. It intercepts incoming requests before they reach a page or API route. Based on the request (its headers, cookies, URL), the middleware can decide to let it pass through, rewrite it to a different destination, redirect the user elsewhere, or block it completely.
How it works
You create a middleware.ts file in the root of your project. This file exports a function that receives the request as a NextRequest object. Inside this function, you can read cookies, headers, and the URL. You then return a NextResponse to control the flow. For performance, you use a matcher config object to specify exactly which paths the middleware should run on, avoiding unnecessary execution on static assets. This code executes in the "Edge Runtime," a lightweight V8 isolate, not a full Node.js environment.
When to use it
Use it for cross-cutting concerns that affect multiple routes. Three common cases are: first, authentication, where you check for a session cookie and redirect to a login page if it's missing; second, A/B testing, where you bucket users by setting a cookie or rewriting the URL to a different page variant; and third, internationalization, where you detect the user's preferred language from a header and redirect them to the correct locale path.
When not to use it
Avoid middleware for heavy, long-running computations, as the Edge Runtime has strict execution time limits. It's also not the place for logic that's specific to a single page; put that logic in the page itself. Finally, because it lacks access to native Node.js APIs, you cannot use it for tasks that require file system access (fs) or other Node-specific dependencies.
One canonical example
A common use case is protecting a dashboard. The middleware is configured to run on all paths starting with /dashboard. It inspects the incoming request's cookies for an auth_token. If the token is missing or invalid, it returns a NextResponse.redirect() to send the user to the /login page. If the token is present, it allows the request to proceed to the dashboard page.
Interview question
Which task is generally NOT recommended for implementation within Next.js Middleware?
- a.Determining a user's locale preference for internationalization
- b.Reading and writing files directly to the server's local storageCorrect
- c.Implementing A/B testing by dynamically rewriting paths
- d.Redirecting users to a login page if they lack authentication
Why? this is the answer
Next.js Middleware runs in a limited Edge Runtime, which lacks access to native Node.js APIs like file system operations (fs). Tasks such as authentication, A/B testing, and internationalization are explicitly mentioned as suitable use cases for middleware.
Just read this? Test yourself on what you have been reading.
Read the original → nextjs.org
- #next.js
- #middleware
- #edge functions
- #routing
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on next.js — each one lists the topics its interview covers.
See open roles