NextJS ~Edge Runtime~

Even though Next.js runs on Node.js, do you know that middleware runs on Edge Runtime?
I introduce you follows:
・ What is Edge Runtime?
・ What is the difference for Node.js Runtime?
・ Can you use Node.js on Runtime?
**The role of middleware**
Middleware, as the name suggests, is a mechanism that processes requests between the client and server. Examples of the purposes for which middleware can be used include:
redirecting to the login screen when accessing the login URL before authentication;
capturing the request data and logging it;
changing the path for each request for A/B testing purposes and returning it.
For more specific examples, please refer to the Next.js official documentation.
**What is Edge Runtime ?**
Next.js middleware runs on Edge Runtime. Edge runtime is the environment in which JavaScript runs with limited functionalities.
Edge Runtime adapts the V8 engine and supports Web APIs such as fetch, Request, and Response .
While it doesn't support Node.js native APIs such as ` fs `, ` http `, and `crypto.`
Therefore, if you need to encrypt authentication credentials within the middleware or access the file system, you cannot use Node.js APIs and must instead consider alternative approaches, such as using external modules or moving the functionality to the application layer. For details on supported APIs, please refer to the Edge Runtime chapter in the official documentation.
Edge Runtime is designed for using edge computing and has been optimized for lightweight performance by carefully selecting only the necessary features.
Edge computing includes:
Vercel Edge Functions
AWS LambdaEDGE
Cloudflare Workers
Most of them are limited by the size of memory or code for optimization performance. For example, Vercel is designed for a limitation of 1MB〜4MB code size or a response within 25s, which causes the timeout error.
**In Next.js v15, we can use Node.js in middleware on runtime enviroment.**
We can use Node.js in middleware on runtime envoriment experimentally.
This allows us to use fs , http .
**Points to Consider*
We can use Node.js's native APIs to add more complex features to the middleware. However, we must consider the impact on performance and resource consumption.
**Keep middleware as simple as possible*
Middleware essentially works on requests or responses. We have to restrict jobs to simple tasks such as header manipulation and redirects, and move heavy work to the server. If we need to use the Node.js native API, use it.





