Skip to content

Next.js

Edge and Node.js Runtimes

Next.js has two server runtimes where you can render parts of your application code: the Node.js Runtime and the Edge Runtime. Depending on your deployment infrastructure, both runtimes support streaming.

By default, Next.js uses the Node.js runtime. Middleware and Edge API Routes use the Edge runtime.

Page Runtime Option

On each page, you can optionally export a runtime config set to either 'nodejs' or 'experimental-edge':

// pages/index.js
export default function Index () { ... }

export function getServerSideProps() { ... }

export const config = {
  runtime: 'experimental-edge',
}

Runtime Differences

Node (Server)Node (Serverless)Edge
Namenodejsnodejsedge or experimental-edge if using Next.js Rendering
Cold Boot/~250msInstant
HTTP StreamingYesYesYes
IOAllAllfetch
Scalability/HighHighest
SecurityNormalHighHigh
LatencyNormalLowLowest
Code Size/50 MB4 MB
NPM PackagesAllAllA smaller subset

Next.js' default runtime configuration is good for most use cases, but there are still many reasons to change to one runtime over the other one.

For example, for API routes that rely on native Node.js APIs, they need to run with the Node.js Runtime. However, if an API only uses something like cookie-based authentication, using Middleware and the Edge Runtime will be a better choice due to its lower latency as well as better scalability.

Edge API Routes

Edge API Routes enable you to build high performance APIs with Next.js using the Edge Runtime.

export const config = {
  runtime: 'edge',
}

export default (req) => new Response('Hello world!')

Related