01 / 10

Describe middleware.js.

Middleware allows you to run code before a request is completed. Then, based on the incoming request, you can modify the response by rewriting, redirecting, modifying the request or response headers, or responding directly.

Use the file middleware.ts (or .js) in the root of your project to define Middleware. For example, at the same level as pages or app, or inside src if applicable.
  1. 1

    While only one middleware.ts file is supported per project, you can still organize your middleware logic modularly. Break out middleware functionalities into separate .ts or .js files and import them into your main middleware.ts file.

  2. 2

    matcher allows you to filter Middleware to run on specific paths.

Difficulty: 7/10
Topics: Edge Runtime, Request Routing, Authentication

Scenario Questions

0-2 years experience
  1. 1

    We want to protect our /dashboard route. If a user doesn't have a specific session cookie, how would you use middleware.js to redirect them to /login?

  2. 2

    You write some code in middleware.js to read a local JSON config file using Node's fs module, but it crashes on startup. Why is this happening, and how would you fix it?

  3. 3

    We noticed our middleware is running on every single static asset request, like images and CSS, which is slowing down page loads. How would you configure it to only run on specific API and page routes?

2-5 years experience
  1. 1

    We are rolling out an A/B test. We want to bucket users based on a cookie and rewrite their request to either /version-a or /version-b under the hood without changing the URL in the browser. How would you implement this in middleware, and how do you ensure it doesn't break CDN caching?

  2. 2

    A developer on your team added a redirect in middleware.js from / to /welcome, but now the browser throws a 'Too many redirects' error. What's causing this infinite loop, and how do you debug and resolve it?

  3. 3

    We need to detect a user's country to redirect them to a localized path like /fr for France. How would you leverage Next.js middleware and the incoming request object to do this efficiently without hitting an external IP lookup API?

5-8 years experience
  1. 1

    We are designing a global application. We need to decide whether to validate JWTs inside middleware.js at the Edge, or defer validation to individual Server Components or API routes. What are the latency, security, and database connection tradeoffs of both approaches?

  2. 2

    Your team wants to integrate a third-party security/logging SDK inside middleware.js. However, the SDK relies on Node.js built-ins and exceeds the Edge runtime's size limits. How would you architect a solution to still intercept requests without bloating or breaking the middleware?

  3. 3

    We need to implement basic IP-based rate limiting for our API routes. If we use middleware.js connected to an external Upstash Redis instance, what happens to our global latency if the Redis instance experiences a spike in response times? How would you design this to fail-safe?

8+ years experience
  1. 1

    We are migrating a massive legacy monolith into a Next.js multi-zone architecture with multiple independent Next.js apps. How do you handle global routing, authentication, and shared headers across these zones using middleware? Would you use a single root middleware, or delegate to zone-specific middlewares, and why?

  2. 2

    We currently use an Nginx reverse proxy layer for routing, basic auth, and header injection. The product team wants to migrate these responsibilities into Next.js middleware.js to give frontend developers more control. What are the architectural risks, performance implications, and operational tradeoffs of moving this logic from the infrastructure layer to the application layer?

Follow-up Questions

  • How does the Edge runtime differ from a standard Node.js runtime in terms of package compatibility?
  • What is the difference between a rewrite and a redirect in Next.js middleware?
  • How would you share state or data between middleware and your downstream API routes or Server Components?