2. Which of the following is the correct way to access the user's country code in Next.js middleware using edge logic?
easy
A. const country = request.geo.country
B. const country = request.location.countryCode
C. const country = request.headers['x-country']
D. const country = request.geoCode.country
Solution
Step 1: Recall Next.js middleware geo API
Next.js provides a geo object on the request with location info.
Step 2: Identify correct property for country code
The correct property is request.geo.country to get the country code.
Final Answer:
const country = request.geo.country -> Option A
Quick Check:
request.geo.country = country code [OK]
Hint: Use request.geo.country to get country code [OK]
Common Mistakes:
Using incorrect property names like geoCode or location
Trying to get country from headers without custom setup
Confusing geo with location objects
3. Given this Next.js middleware code snippet, what will be the redirect URL if the user is from 'US'?
export function middleware(request) {
const country = request.geo?.country || 'unknown';
if (country === 'US') {
return Response.redirect(new URL('/us-home', request.url));
}
return Response.next();
}
medium
A. /unknown
B. /home
C. /us-home
D. /
Solution
Step 1: Check country value from request
The code sets country to request.geo?.country or 'unknown'. For a US user, it is 'US'.
Step 2: Analyze redirect condition
If country is 'US', the middleware redirects to '/us-home'. Otherwise, it continues normally.
Final Answer:
/us-home -> Option C
Quick Check:
Country 'US' triggers redirect to /us-home [OK]
Hint: Country 'US' redirects to /us-home [OK]
Common Mistakes:
Ignoring the redirect condition
Assuming default path is used for US
Confusing Response.next() with redirect
4. Identify the error in this Next.js middleware code that tries to redirect users from Canada to '/ca-home':
export function middleware(request) {
const country = request.geo.country;
if (country = 'CA') {
return Response.redirect(new URL('/ca-home', request.url));
}
return Response.next();
}
medium
A. Missing optional chaining on request.geo
B. Using assignment '=' instead of comparison '===' in the if condition
C. Response.redirect should be Response.redirectTo
D. URL constructor is used incorrectly
Solution
Step 1: Check the if condition syntax
The condition uses country = 'CA', which assigns 'CA' instead of comparing.
Step 2: Identify correct comparison operator
It should use === to compare values, not =.
Final Answer:
Using assignment '=' instead of comparison '===' in the if condition -> Option B
Quick Check:
Use '===' for comparison, not '=' [OK]
Hint: Use '===' for comparison, not '=' [OK]
Common Mistakes:
Confusing assignment and comparison operators
Thinking Response.redirectTo exists
Overlooking optional chaining necessity
5. You want to serve different homepage content for users from Europe and Asia using Next.js edge middleware. Which approach correctly implements this logic?