Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of the middleware.ts file in Next.js?
The middleware.ts file runs code before a request is completed. It can modify requests, responses, or redirect users. It helps control access and behavior globally or for specific routes.
Click to reveal answer
beginner
Where should the middleware.ts file be placed in a Next.js project?
The middleware.ts file should be placed in the root of the Next.js project, at the same level as pages or app folders. This placement ensures it runs for all routes by default.
Click to reveal answer
intermediate
How do you export middleware in middleware.ts?
You export a function named <code>middleware</code> that takes a <code>NextRequest</code> and returns a <code>NextResponse</code> or modifies the request. Example: <br><pre>import { NextRequest, NextResponse } from 'next/server';
export function middleware(request: NextRequest) {
return NextResponse.next();
}</pre>
Click to reveal answer
intermediate
What is the role of the matcher export in middleware.ts?
The matcher export defines which paths the middleware should run on. It is an array of strings or patterns. This helps limit middleware to specific routes instead of all routes.
Click to reveal answer
beginner
Why should middleware code be fast and lightweight?
Middleware runs on every matching request before the page loads. Slow middleware delays the whole page response. Keeping it fast ensures a smooth user experience and better performance.
Click to reveal answer
Where do you place the middleware.ts file in a Next.js project?
AInside the <code>components</code> folder
BIn the root folder of the project
CInside the <code>public</code> folder
DInside the <code>styles</code> folder
✗ Incorrect
The middleware.ts file should be placed in the root folder to apply middleware globally or selectively.
What does the middleware function receive as its first argument?
ANextRequest object
BNextResponse object
CHTTP response status code
DReact component props
✗ Incorrect
The middleware function receives a NextRequest object representing the incoming request.
How do you limit middleware to run only on certain routes?
A. Missing import of NextRequest from 'next/server'
B. Accessing cookies directly as an object instead of using req.cookies.get()
C. Using arrow function instead of regular function
D. Not exporting config.matcher for route matching
Solution
Step 1: Check cookie access method
In Next.js middleware, cookies are accessed via req.cookies.get('name'), not as object properties.
Step 2: Identify error cause
Using req.cookies.token will be undefined or cause error; correct is req.cookies.get('token').
Final Answer:
Accessing cookies directly as an object instead of using req.cookies.get() -> Option B
Quick Check:
Use req.cookies.get('token') to access cookies [OK]
Hint: Use req.cookies.get('name') to read cookies in middleware [OK]
Common Mistakes:
Accessing cookies as properties instead of using get() method
Forgetting to import NextResponse
Assuming arrow functions are invalid in middleware
5. You want your middleware.ts to run only on API routes starting with /api/private and redirect users without a valid auth cookie to /api/auth/unauthorized. Which config and middleware code correctly implements this?