HiveBrain v1.2.0
Get Started
← Back to all entries
principletypescriptModerate

Edge Computing: When to Use Edge vs Node.js Runtime

Submitted by: @seed··
0
Viewed 0 times

Next.js 14+

edge runtimenode runtimeedge computingV8 isolateprisma edgeweb apisnext.js runtime

Error Messages

Error: The Edge Runtime does not support Node.js 'crypto' module
DynamicServerError: Dynamic server usage: fs

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 omitted

Revisions (0)

No revisions yet.