Next.jsはサーバーコードを2つのランタイムで実行できます。Node.jsランタイム(デフォルト)は完全なNode環境であり、EdgeランタイムはV8ベースの軽量環境で、ユーザーに近いグローバルに分散されたエッジロケーションで実行されます。
ランタイムの選択
ts
// per route handler / page
export const runtime = "edge"; // or "nodejs" (the default)
Node.js runtime Edge runtime
────────────────────────────────────────────────────────────────
APIs full Node (fs, crypto, Web APIs only (fetch,
Buffer, native modules) Request/Response) — limited
npm packages all only those without Node APIs
Cold start slower, heavier near-zero, lightweight
Location regional (one region) global (near the user)
Latency to user higher if far away very low (geo-distributed)
Execution limits longer, more memory short CPU time, less memory
Database direct TCP connections HTTP-based DBs only (no raw TCP)
✓ Middleware (auth checks, redirects, geo-routing) — must be fast & global
✓ Simple, latency-sensitive endpoints (personalization, A/B, feature flags)
✓ Reading geolocation/headers to tailor responses
Edgeはユーザーに近いことがメリットになる軽量なロジックと即座のコールドスタートに優れています。
✓ Direct database connections (Prisma over TCP, etc.)
✓ Heavy computation, large dependencies, native modules
✓ Anything using Node-specific APIs (fs, crypto.randomBytes via Node, Buffer)
export const runtime = "edge";
import fs from "fs"; // ❌ not available on Edge — build/runtime error
import { PrismaClient } from "@prisma/client"; // ⚠️ needs HTTP/data-proxy on Edge
HTTPベースのアダプタなしではEdgeで動作しない多くの一般的なライブラリ(生のDBソケットを開くORMを含む)があります。これはよくある落とし穴です。
ランタイムの選択は本当のアーキテクチャ決定です:Edgeは高速でグローバルで軽量なロジック(特にミドルウェア)に向いており、コールドスタートが最小限です。Node.jsは完全なAPIアクセス、直接DB接続、および重い作業に向いています。
Edgeの制限(Web APIのみ、生のTCPなし、短い実行時間)を理解することで、データベース負荷の多いコードやNode依存のコードをサポートできないランタイムに配置するという頻繁な誤りを防ぎます。