Edge Runtime
Understand the Next.js Edge Runtime — when to use it, its constraints, and how it enables globally distributed ultra-low-latency responses.
Last reviewed: January 2025
🔍 What Is It?
The Edge Runtime is a lightweight JavaScript runtime that:
- →Runs at CDN edge nodes — 100+ locations globally, close to users
- →Uses Web APIs — fetch, Request, Response, Headers, crypto (Web Crypto)
- →Does NOT support Node.js APIs — no fs, no net, no most npm packages
- →Starts instantly — no cold starts (unlike Node.js serverless functions)
- →Has memory limits — 128MB max (vs 1GB+ for Node.js functions)
Opt into Edge Runtime:
export const runtime = 'edge'; // In a Route Handler or page🎯 Why Do We Need It?
Node.js serverless functions have two problems:
- Cold starts — first request after idle period takes 200–800ms to initialise
- Single region — a function in us-east-1 serves users in Tokyo with 150ms latency
Edge Runtime solves both:
- →No cold starts — always warm
- →Globally distributed — Tokyo users served from Tokyo edge node
🏗️ What Happens in Real Projects?
Edge Runtime use cases:
- →Middleware — auth redirects, A/B testing (always Edge)
- →Personalisation — serve different content based on geolocation
- →Simple API endpoints — no DB access, pure computation
- →Rate limiting — count requests at the edge with Upstash Redis
- →JWT verification — verify tokens without a DB lookup
Do NOT use Edge Runtime for:
- →Database queries (most drivers require Node.js)
- →File system operations
- →Heavy computation (memory limits)
⚙️ How It Works
Edge vs Node.js runtime comparison:
| Feature | Edge | Node.js |
|---|---|---|
| Cold start | None | 200–800ms |
| Location | Global (100+ nodes) | Single region |
| Node.js APIs | No | Yes |
| npm packages | Limited | All |
| Memory | 128MB | 1GB+ |
| Execution time | 30s max | 300s max |
| Database | Edge-compatible only | All drivers |
💻 Example
Edge Runtime Patterns — Edge Route Handler and geolocation-based personalisation.
🤔 Common Confusions
❓ Can I use Prisma in the Edge Runtime?
Standard Prisma requires Node.js. Prisma Accelerate provides an edge-compatible HTTP-based client. Alternatively, use Neon's serverless driver (edge-compatible), Turso (SQLite at the edge), or Upstash Redis for edge-compatible data storage.
❓ Should I make all my Route Handlers Edge Runtime?
No — only routes that genuinely benefit from global distribution and have no Node.js dependencies. Routes that query a database in a single region gain nothing from Edge Runtime (the DB round trip dominates latency). Use Edge for auth, personalisation, and computation-only endpoints.
❓ Does Edge Runtime support WebSockets?
No — Edge Functions are request/response based. For WebSockets, use a dedicated WebSocket service (Pusher, Ably, Supabase Realtime) or Cloudflare Durable Objects.
🚨 Common Production Issues
npm package not compatible with Edge Runtime — many packages use Node.js APIs internally. Check the package's edge compatibility before using it. The build will fail with a clear error if an incompatible package is used.
Memory limit exceeded — Edge Functions have a 128MB memory limit. Large in-memory operations (parsing huge JSON, image processing) will fail. Move heavy operations to Node.js functions.
Geolocation not available in development — request.geo is only populated on Vercel's edge network. In development, it is undefined. Always handle the undefined case with a fallback.
✅ Best Practices
Use Edge Runtime for middleware, auth verification, and geolocation-based routing
Use Node.js runtime (default) for anything that touches a database
Always handle undefined geolocation values — not available in development
Test Edge Runtime compatibility with next build — incompatible packages cause build errors
Use Upstash Redis for edge-compatible rate limiting and session storage
🏛️ Architect's Note
Edge Runtime is a powerful tool for the right use cases. The key architectural question: does this code need to run close to the user, or close to the data? Auth verification and personalisation benefit from edge proximity. Database queries benefit from proximity to the database. Do not force Edge Runtime on DB-dependent routes.
💬 Frequently Asked Questions
🎯 Key Takeaways
🔗 Related Topics
🎯 Practice this topic in interviews
↗ Where this topic fits
Next.js Technology Guide
Return to the Next.js learning hub.
Frontend Engineer Career Path
A beginner-friendly journey through web foundations, React, production frontend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Sources
Sources / References
Use these Next.js references to verify the concept in official docs and continue into the most relevant SplashRide pages.
Official documentation
Related SplashRide pages
SplashRide Next.js learning path
Browse the full Next.js topic collection and learning order on SplashRide.
Next.js Interview Prep
Practice Next.js interview questions and scenario-based answers.
Frontend Engineer Career Path
A beginner-friendly journey through web foundations, React, production frontend engineering, and interview preparation.
Full Stack Java Developer Career Path
A guided route through frontend foundations, Java backend engineering, delivery workflows, and interview readiness.
Middleware
Master Next.js Middleware — the edge function that runs before every request, enabling auth redirects, A/B testing, geolocation, and request modification at the CDN edge.
Route Handlers
Master Route Handlers in Next.js — the App Router replacement for API routes, supporting all HTTP methods, streaming responses, and edge runtime.