0
0
NextJSframework~10 mins

Edge runtime vs Node.js runtime in NextJS - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Edge runtime vs Node.js runtime
Request Received
Check Runtime Type
Edge Runtime
Run Lightweight
Fast, Limited
Send Response
When a request comes in, Next.js decides to run it on Edge runtime or Node.js runtime. Edge runs fast with limited APIs, Node.js runs slower but supports full Node features.
Execution Sample
NextJS
export const runtime = 'edge';

export function GET(request) {
  return new Response('Hello from Edge!');
}
This code runs a simple API route on the Edge runtime, returning a fast response.
Execution Table
StepActionRuntimeAPIs AvailablePerformanceOutput
1Request receivedCheck runtimeN/AN/AN/A
2Determine runtime from exportEdgeLimited Web APIsVery fastPrepare response
3Run handler functionEdgeFetch, Request, ResponseVery fast'Hello from Edge!'
4Send responseEdgeN/AVery fastResponse sent to client
5Request receivedCheck runtimeN/AN/AN/A
6Determine runtime from exportNode.jsFull Node.js APIsSlowerPrepare response
7Run handler functionNode.jsFile system, process, etc.Slower'Hello from Node.js!'
8Send responseNode.jsN/ASlowerResponse sent to client
9EndN/AN/AN/ANo more requests
💡 Execution stops after sending response to client.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6After Step 7After Step 8Final
runtimeundefined'edge''edge''edge''nodejs''nodejs''nodejs'undefined
responseundefinedundefined'Hello from Edge!''Hello from Edge!'undefined'Hello from Node.js!''Hello from Node.js!'undefined
Key Moments - 2 Insights
Why does the Edge runtime have limited APIs compared to Node.js?
Edge runtime runs in a lightweight environment close to the browser, so it only supports web-standard APIs like fetch and Response, unlike Node.js which supports full server APIs. See execution_table rows 2 and 6.
How does specifying 'export const runtime = "edge"' affect the code execution?
This export tells Next.js to run the code on the Edge runtime, enabling faster execution but limiting available APIs. This is shown in execution_table rows 2-4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what APIs are available at Step 3 when running on Edge runtime?
AFull Node.js APIs like file system
BNo APIs available
CLimited Web APIs like fetch, Request, Response
DOnly database APIs
💡 Hint
Check the 'APIs Available' column at Step 3 in the execution_table.
At which step does the Node.js runtime run the handler function?
AStep 7
BStep 4
CStep 3
DStep 2
💡 Hint
Look for 'Run handler function' with 'Node.js' runtime in the execution_table.
If you remove 'export const runtime = "edge"', which runtime will Next.js use by default?
AEdge runtime
BNode.js runtime
CBrowser runtime
DNo runtime
💡 Hint
Default runtime is Node.js if no runtime export is specified, see variable_tracker 'runtime' after Step 6.
Concept Snapshot
Next.js can run code on two runtimes:
- Edge runtime: fast, limited web APIs, runs close to user
- Node.js runtime: slower, full Node.js APIs
Specify runtime with 'export const runtime = "edge"'
Edge is great for fast responses; Node.js for full server features.
Full Transcript
This visual execution compares Edge runtime and Node.js runtime in Next.js. When a request comes in, Next.js checks the runtime type. If 'export const runtime = "edge"' is set, it runs on Edge runtime using limited web APIs like fetch and Response, resulting in very fast execution. Otherwise, it runs on Node.js runtime, which supports full Node.js APIs but is slower. The execution table shows steps for both runtimes handling requests and sending responses. Variables like 'runtime' and 'response' change accordingly. Key moments clarify why Edge runtime is limited and how specifying runtime affects execution. The quiz tests understanding of API availability, runtime steps, and default behavior. The snapshot summarizes the differences and usage of both runtimes.