principletypescriptModerate
Edge Computing: When to Use Edge vs Node.js Runtime
Viewed 0 times
Next.js 14+
edge runtimenode runtimeedge computingV8 isolateprisma edgeweb apisnext.js runtime
Error Messages
Problem
Developers deploy all middleware and API routes to the edge runtime without understanding its limitations, then encounter missing Node.js APIs, incompatible npm packages, or unexpected behavior from stateless execution.
Solution
Use the edge runtime for latency-sensitive, stateless work close to users: authentication checks, geolocation-based redirects, A/B testing, and request rewriting. Keep database queries, file operations, and Node.js-dependent libraries in the Node.js runtime.
Why
Edge runtimes (Cloudflare Workers, Vercel Edge) run V8 isolates in 30+ global locations with <1ms cold starts. They implement Web APIs only. Node.js runtime runs in fewer regions but supports the full Node.js ecosystem including native modules and filesystem access.
Gotchas
- Prisma's main client is not edge-compatible — use @prisma/adapter-neon or the Accelerate proxy for edge database access
- Edge functions cannot establish long-lived connections — connection pooling (Neon serverless, PgBouncer) is mandatory
- Runtime mismatches surface as runtime errors in production that do not appear in local development
Code Snippets
Specifying runtime in Next.js route segments
// app/api/auth/route.ts — edge compatible (stateless JWT check)
export const runtime = 'edge';
// app/api/users/route.ts — needs Prisma, stays on Node
export const runtime = 'nodejs'; // default, can be omittedRevisions (0)
No revisions yet.