Challenge - 5 Problems
Geolocation Edge Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Next.js Edge Middleware handle geolocation-based redirects?
Given this Next.js Edge Middleware snippet, what will happen when a user from France visits the site?
import { NextResponse } from 'next/server';
export function middleware(request) {
const country = request.geo?.country || 'US';
if (country === 'FR') {
return NextResponse.redirect(new URL('/fr', request.url));
}
return NextResponse.next();
}NextJS
import { NextResponse } from 'next/server'; export function middleware(request) { const country = request.geo?.country || 'US'; if (country === 'FR') { return NextResponse.redirect(new URL('/fr', request.url)); } return NextResponse.next(); }
Attempts:
2 left
💡 Hint
Check how the country code is used to decide the redirect.
✗ Incorrect
The middleware checks the user's country from request.geo. If it is 'FR', it redirects to '/fr'. Otherwise, it continues normally.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Next.js Edge Middleware geolocation code
What is wrong with this middleware code snippet?
export function middleware(request) {
const country = request.geo.country;
if country === 'US' {
return NextResponse.redirect('/us');
}
return NextResponse.next();
}NextJS
export function middleware(request) { const country = request.geo.country; if (country === 'US') { return NextResponse.redirect('/us'); } return NextResponse.next(); }
Attempts:
2 left
💡 Hint
Check the syntax of the if statement.
✗ Incorrect
In JavaScript, the if statement requires parentheses around the condition: if (condition) { ... }
❓ state_output
advanced2:00remaining
What is the value of 'region' after this middleware runs for a user in California?
Consider this middleware code:
What will be the response body if the user is located in California, USA?
export function middleware(request) {
const region = request.geo?.region || 'unknown';
return new Response(region);
}What will be the response body if the user is located in California, USA?
NextJS
export function middleware(request) { const region = request.geo?.region || 'unknown'; return new Response(region); }
Attempts:
2 left
💡 Hint
Check the format of the region property in request.geo.
✗ Incorrect
The region property in request.geo returns the region code, e.g., 'CA' for California, not the full name.
🔧 Debug
advanced2:00remaining
Why does this Next.js Edge Middleware fail to redirect based on geolocation?
Review this middleware code:
Users from Germany are not redirected. What is the likely cause?
import { NextResponse } from 'next/server';
export function middleware(request) {
if (request.geo.country === 'DE') {
return NextResponse.redirect(new URL('/de', request.url));
}
return NextResponse.next();
}Users from Germany are not redirected. What is the likely cause?
NextJS
import { NextResponse } from 'next/server'; export function middleware(request) { if (request.geo.country === 'DE') { return NextResponse.redirect(new URL('/de', request.url)); } return NextResponse.next(); }
Attempts:
2 left
💡 Hint
Check if the middleware runs on the expected routes.
✗ Incorrect
Without specifying a matcher in the middleware config, the middleware may not run on any route, so no redirect happens.
🧠 Conceptual
expert2:00remaining
How does Next.js Edge Middleware geolocation data affect caching strategies?
You want to serve different content based on user country using Next.js Edge Middleware. Which statement about caching is correct?
Attempts:
2 left
💡 Hint
Think about how edge caches differentiate content by headers.
✗ Incorrect
To serve country-specific content correctly, caches must vary by the header that carries geolocation info, like 'x-vercel-ip-country'.