Hint: Use NextResponse.next() to proceed to API handler [OK]
Common Mistakes:
Using NextResponse.stop() which blocks the request
Confusing redirect() with continuing
Forgetting to return NextResponse.next()
3. Given this middleware code, what will happen when a request with header x-auth: secret is sent?
import { NextResponse } from 'next/server';
export function middleware(request) {
if (request.headers.get('x-auth') !== 'secret') {
return NextResponse.redirect(new URL('/unauthorized', request.url));
}
return NextResponse.next();
}
medium
A. The request is redirected to /unauthorized
B. The request continues to the API route handler
C. The middleware throws an error
D. The request is blocked with no response
Solution
Step 1: Check header condition
The middleware checks if 'x-auth' header equals 'secret'. If yes, it continues.
Step 2: Analyze given header
The request has 'x-auth: secret', so condition is false and middleware returns NextResponse.next().
Final Answer:
The request continues to the API route handler -> Option B
Quick Check:
Header matches 'secret' so continue [OK]
Hint: Check header value to decide redirect or continue [OK]
Common Mistakes:
Assuming redirect happens even if header matches
Thinking middleware throws error on mismatch
Ignoring header case sensitivity
4. Identify the error in this Next.js middleware code:
import { NextResponse } from 'next/server';
export function middleware(request) {
if (!request.headers.get('authorization')) {
NextResponse.redirect('/login');
}
return NextResponse.next();
}
medium
A. Using 'authorization' header instead of 'auth'
B. Middleware function must be async
C. NextResponse.next() should be inside the if block
D. Missing return before NextResponse.redirect
Solution
Step 1: Check redirect usage
NextResponse.redirect must be returned to stop further processing.
Step 2: Identify missing return
The code calls NextResponse.redirect but does not return it, so middleware continues incorrectly.
Final Answer:
Missing return before NextResponse.redirect -> Option D
Quick Check:
Always return redirect response [OK]
Hint: Always return redirect to stop middleware flow [OK]
Common Mistakes:
Not returning redirect response
Thinking middleware must be async
Misplacing NextResponse.next() inside if block
5. You want to create middleware that blocks requests to API routes if the query parameter token is missing or empty. Which code correctly implements this behavior?
hard
A. export function middleware(request) {
const url = new URL(request.url);
if (!url.searchParams.get('token')) {
return NextResponse.redirect(new URL('/error', request.url));
}
return NextResponse.next();
}
B. export function middleware(request) {
if (!request.query.token) {
return NextResponse.redirect('/error');
}
return NextResponse.next();
}
C. export function middleware(request) {
if (request.url.token === '') {
return NextResponse.next();
}
return NextResponse.redirect('/error');
}
D. export function middleware(request) {
const token = request.headers.get('token');
if (!token) {
return NextResponse.next();
}
return NextResponse.redirect('/error');
}
Solution
Step 1: Access query parameters correctly
Use new URL(request.url) and url.searchParams.get('token') to read query params.
Step 2: Check token presence and redirect if missing
If token is missing or empty, redirect to /error; otherwise continue with NextResponse.next().
Final Answer:
Code that checks query param and redirects if missing -> Option A
Quick Check:
Use URL and searchParams for query checks [OK]
Hint: Use URL and searchParams to check query tokens [OK]
Common Mistakes:
Trying to access query params directly on request
Checking headers instead of query parameters
Reversing redirect and continue logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.