Discover how running code closer to users can make your website feel instant!
Edge runtime vs Node.js runtime in NextJS - When to Use Which
Imagine you have a website that needs to respond quickly to users all over the world. You try to run your code on a single server far away from many users.
Every time someone visits, the server takes time to process and send back the response.
Using only a traditional Node.js server means requests from distant users travel far, causing delays.
Also, Node.js servers can be heavy and slower to start, making your site feel sluggish.
Edge runtimes run your code closer to users, at many locations worldwide, making responses faster.
They start quickly and handle simple tasks efficiently, improving user experience.
app.listen(3000, () => console.log('Server running'))
export const runtime = 'edge'; export default function handler(req) { return new Response('Fast response'); }
It enables lightning-fast responses by running code near users, improving speed and scalability.
A global news site uses edge runtime to deliver breaking news instantly to readers everywhere without delay.
Node.js runs on central servers, which can slow down distant users.
Edge runtime runs code near users, speeding up responses.
Choosing the right runtime improves website speed and user happiness.
Practice
Edge runtime in Next.js?Solution
Step 1: Understand Edge runtime purpose
The Edge runtime runs code on servers geographically closer to users, reducing latency.Step 2: Compare with Node.js runtime
Unlike Node.js runtime, Edge runtime focuses on speed by running near users but has some limitations on Node features.Final Answer:
It runs code closer to users for faster response times. -> Option AQuick Check:
Edge runtime = faster user response [OK]
- Thinking Edge runtime supports all Node.js modules
- Confusing Edge runtime with full Node.js capabilities
- Assuming Edge runtime caches all responses automatically
Solution
Step 1: Recall syntax for setting runtime
Next.js requires exporting a constant namedruntimewith value 'edge' or 'nodejs'.Step 2: Identify correct export statement
The correct syntax isexport const runtime = 'edge';to set Edge runtime.Final Answer:
export const runtime = 'edge'; -> Option DQuick Check:
Useexport const runtime = 'edge'[OK]
- Using default export instead of named export
- Assigning runtime without export
- Setting runtime to 'nodejs' when Edge is needed
export const runtime = 'edge'; is set?export const runtime = 'edge';
export default function handler(req) {
return new Response('Hello from Edge!');
}Solution
Step 1: Check the runtime export
The code explicitly setsruntime = 'edge', so it uses the Edge runtime environment.Step 2: Understand Edge runtime features
Edge runtime supports limited Node.js APIs and runs close to users for speed, unlike full Node.js runtime.Final Answer:
Edge runtime with limited Node APIs -> Option BQuick Check:
runtime = 'edge' means Edge runtime [OK]
- Assuming full Node.js APIs are available in Edge runtime
- Confusing Edge runtime with client-side code
- Thinking no runtime is used
export const runtime = 'edge';
import fs from 'fs';
export default function handler(req) {
const data = fs.readFileSync('/data.txt', 'utf8');
return new Response(data);
}Solution
Step 1: Check module usage in Edge runtime
Edge runtime does not support Node.js built-in modules like 'fs' because it runs in a restricted environment.Step 2: Identify the error cause
Importing and using 'fs' causes a runtime error in Edge runtime; this is not allowed.Final Answer:
Edge runtime does not support importing 'fs' module. -> Option AQuick Check:
Edge runtime forbids 'fs' module [OK]
- Thinking async is required here
- Believing Response usage is wrong
- Assuming runtime setting is incorrect
crypto for hashing and runs as close to users as possible. Which runtime should you choose and why?Solution
Step 1: Understand crypto support in runtimes
Edge runtime supports the Web Crypto API natively, which can be used for hashing, while Node.js runtime supports Node's crypto module.Step 2: Choose runtime for proximity and crypto
Since you want to run close to users and use crypto, Edge runtime is best because it supports Web Crypto and runs near users.Final Answer:
Edge runtime, because it supports Web Crypto API and runs near users. -> Option CQuick Check:
Edge runtime + Web Crypto API = best choice [OK]
- Assuming Edge runtime lacks any crypto support
- Choosing Node.js runtime despite wanting low latency
- Thinking manual polyfill is needed for crypto in Edge
Ready to prove it?
Pass the test and this pattern moves unknown
