Module A-9·25 min read

Edge runtime constraints and size limits, the Prisma/Edge incompatibility and why Drizzle + Neon serverless is the fix, middleware performance profiling, feature flag architecture with GrowthBook at the edge, A/B testing without layout shift, and geo-routing at the CDN layer.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

A-9 — Edge Compute, Feature Flags, and Geo-Routing

Who this is for: Architects building applications that need to behave differently based on who's requesting — where they are in the world, what experiment they're in, what role they hold. This module is about doing that decision-making at the edge, before the request ever reaches your Next.js server, so the cost of personalisation is milliseconds not seconds.


What "Edge" Actually Means in Practice

"Edge compute" has become a buzzword. The concrete meaning: code that runs in V8 isolates deployed at CDN edge nodes globally — not in a central server region. A request from Tokyo runs your edge code in a Tokyo datacenter, not in us-east-1.

The V8 isolate model has specific properties:

  • Near-zero cold start — V8 isolates start in microseconds, not the 100-300ms of a Node.js cold start
  • Geographic proximity — code runs close to users, eliminating cross-ocean RTT
  • No Node.js APIsfs, crypto, most npm packages don't work. Only Web Standard APIs
  • 1MB bundle limit — your Middleware code must be tiny

In Next.js, edge compute surfaces in two places:

  1. Middleware (middleware.ts) — runs at the edge for every request before routing
  2. Edge Route Handlers — individual Route Handlers opted into runtime = 'edge'

Middleware is the primary edge primitive. It's where geo-routing, feature flags, and auth checks belong — before the request reaches any Server Component.


Middleware Architecture — What to Put There

Middleware is fast and runs everywhere. That makes it tempting to put everything in it. The discipline: Middleware is for routing decisions, not business logic.

The right things to do in Middleware:

  • Read a cookie or header and rewrite/redirect the request
  • Check authentication status and redirect to login
  • Set response headers (security headers, CORS)
  • Geo-routing (redirect based on country)
  • A/B test assignment (assign a variant cookie, rewrite to a variant path)

The wrong things to do in Middleware:

  • Database queries (no Prisma, no connection pooling at the edge)
  • Complex cryptographic operations
  • Importing large npm packages
  • Business logic that should be in Server Components
ts

The matcher config limits which paths Middleware runs on. This is critical for performance — running Middleware on _next/static requests is wasteful. The regex /((?!api|_next/static|_next/image|favicon.ico).*) excludes all static assets.


Geo-Routing in Production

Vercel injects the user's geographic data into the request as headers. NextRequest.geo was removed from Next.js 15 — read this data with geolocation(request) from the @vercel/functions package instead. It returns:

  • country — ISO 3166-1 alpha-2 country code (e.g., 'US', 'DE', 'JP')
  • region — region/state code (e.g., 'CA' for California)
  • city — city name
  • latitude / longitude — approximate coordinates
ts

The difference between redirect and rewrite:

  • redirect — changes the URL in the browser. User sees they're on a different domain/path.
  • rewrite — serves different content but keeps the same URL. Invisible to the user.

Rewrites are the right choice for localised content at the same URL. Redirects are right when users genuinely need to be on a different domain (legal requirement, different service).


Feature Flags at the Edge

Feature flags at the edge — assigning flag values in Middleware and reading them in Server Components — is the architecture that eliminates flicker and layout shift from client-side feature flags.

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.