0
0
NextJSframework~20 mins

Geolocation and edge logic in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Geolocation Edge Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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();
}
AThe user stays on the original page without redirect.
BThe user is redirected to '/fr' path.
CThe middleware throws an error because 'geo' is undefined.
DThe user is redirected to '/us' path.
Attempts:
2 left
💡 Hint
Check how the country code is used to decide the redirect.
📝 Syntax
intermediate
2: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();
}
AMissing parentheses around the if condition.
BNextResponse.redirect requires a URL object, not a string.
Crequest.geo is not a valid property.
DThe function must be async to use request.geo.
Attempts:
2 left
💡 Hint
Check the syntax of the if statement.
state_output
advanced
2:00remaining
What is the value of 'region' after this middleware runs for a user in California?
Consider this middleware code:
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);
}
A"CA"
B"California"
C"unknown"
DAn empty string
Attempts:
2 left
💡 Hint
Check the format of the region property in request.geo.
🔧 Debug
advanced
2:00remaining
Why does this Next.js Edge Middleware fail to redirect based on geolocation?
Review this middleware code:
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();
}
AThe middleware function must be async to access request.geo.
Brequest.geo.country is undefined because geo is not supported on Edge.
CThe middleware is missing the 'config' export to specify matcher paths.
DNextResponse.redirect requires a full URL, not a path string.
Attempts:
2 left
💡 Hint
Check if the middleware runs on the expected routes.
🧠 Conceptual
expert
2: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?
ACaching is disabled automatically when using request.geo in middleware.
BYou can cache the response globally without variation because geolocation is client-side.
CNext.js automatically creates separate cache entries per country without extra config.
DYou must vary the cache by the 'x-vercel-ip-country' header to avoid serving wrong content.
Attempts:
2 left
💡 Hint
Think about how edge caches differentiate content by headers.