0
0
NextJSframework~10 mins

Geolocation and edge logic in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Geolocation and edge logic
User Request
Edge Middleware runs
Check Geolocation from request
Apply Edge Logic
Modify Response or Redirect
Send Response to User
The flow starts with a user request hitting the edge middleware, which checks geolocation data. Based on location, it applies edge logic or defaults, then sends the response.
Execution Sample
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();
}
This middleware checks the user's country from geolocation and redirects French users to a French page, others continue normally.
Execution Table
StepRequest Geo CountryCondition (country === 'FR')ActionResult
1FRTrueRedirect to /frUser redirected to /fr
2USFalseContinueUser proceeds normally
3undefinedFalseContinueUser proceeds normally with default US
💡 Execution stops after redirect or continuing to next middleware/handler.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
countryundefinedFRUSUS (default)
Key Moments - 2 Insights
Why does the middleware redirect only when country is 'FR'?
Because the condition checks if country === 'FR' (see execution_table step 1). Only then it triggers a redirect; otherwise, it continues.
What happens if geolocation data is missing?
The code uses 'US' as a default (execution_table step 3), so it continues without redirect.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the action when the country is 'US'?
ARedirect to /fr
BRedirect to /us
CContinue normally
DThrow an error
💡 Hint
Check execution_table row 2 under 'Action' column.
At which step does the condition country === 'FR' evaluate to true?
AStep 1
BStep 2
CStep 3
DNever
💡 Hint
See execution_table 'Condition' column for each step.
If the default country was changed to 'DE', what would happen at step 3?
ARedirect to /de
BContinue normally
CRedirect to /fr
DThrow an error
💡 Hint
Default country affects variable_tracker and condition check in execution_table step 3.
Concept Snapshot
Next.js Edge Middleware can access user geolocation via request.geo.
Use this to apply edge logic like redirects or content changes.
Check if request.geo?.country matches a target country.
Redirect or modify response accordingly.
Fallback to default if geolocation is missing.
This runs at the edge, before page rendering.
Full Transcript
This example shows how Next.js Edge Middleware uses geolocation data from the request to decide what to do. When a user sends a request, the middleware runs first at the edge. It checks the country code from request.geo. If the country is 'FR', it redirects the user to the French page '/fr'. If the country is 'US' or geolocation is missing, it continues normally. The variable 'country' tracks the detected or default country. This logic helps serve location-specific content quickly and efficiently at the edge.