Replies: 18 comments 26 replies
-
This is amazing, thank you! After having switched over an application of mine to the new This means that the "1 MB" threshold mentioned above has now been crossed:
|
Beta Was this translation helpful? Give feedback.
-
This is going to be a great addition to Next.js! I have a few questions though. Are you planning to run a real V8 or edge environment on development? This way errors are realistic if you try to use an unsupported API or any edge case that happens only in the production environment. Also, the |
Beta Was this translation helpful? Give feedback.
-
Have been digging into Next and really liking it so far in the context of evaluating it for our business needs and the Middleware API is great! However, the limitations they impose as per the Edge Runtime restrictions... not so much. 😅 So it was great to see this Discussion in my searching as this limiting factor seems to come up often as a frequent topic when doing some casual searching of recent posts
Presumably, with the option to specify the If not, is that something the team would consider and / or is there a way to turn "off" the Edge Runtime in the interim? Is that what something like next-connect is for? Thanks! |
Beta Was this translation helpful? Give feedback.
-
Is this coming soon? |
Beta Was this translation helpful? Give feedback.
-
Any updates on this? 🥹 |
Beta Was this translation helpful? Give feedback.
-
Sorry for the slow response everyone -- yes, it's out as an experimental feature - docs can be seen here! |
Beta Was this translation helpful? Give feedback.
-
@jescalan It is not working. I tried: // next.config.js
module.exports = {
experimental: {
runtime: 'nodejs',
},
} Also tried, import { NextResponse } from 'next/server';
import { writeFile, readFile, appendFile } from 'fs/promises';
export async function middleware(req) {
appendFile(/* Do some stuff*/);
return NextResponse.next();
}
export const config = {
runtime: 'nodejs',
}
When printing the ENV variable, it is |
Beta Was this translation helpful? Give feedback.
-
As @martinjlowm mentioned here #34179 (reply in thread). The experimental flag does not apply to the runtime for middleware. Any feedback on that? |
Beta Was this translation helpful? Give feedback.
-
@Jelle-vz Hey!
), maybe it's not the perfect approach , but I was really surprised when I could use neither: |
Beta Was this translation helpful? Give feedback.
-
@shuding I've successfully tested the edge runtime to work with other V8-based (edge) providers like Cloudflare Pages / Workers, but I feel like the build output could be optimized to provide more flexibility and efficiency on where you deploy Next.js on the edge. In the current implementation of the build export of Next.js, each function is implemented as its own individual server with only the routes for that one specific page or API endpoint. On platforms like Cloudflare, they require you to bundle everything into one single Worker. This means for it to run properly there, you'd need to write your own, custom router. These individual functions also cause side-effects, currently in next.js/packages/next/server/web/adapter.ts Line 217 in 4cd8b23 In an experimental implementation of an edge function that bundles these, I essentially import all the edge functions into a single one based on Preferably, I'd love to see these pages exported more efficiently, maybe as truly individual pages with a separate file for the router/server, so that other tools and platforms can provide their own routers for it. The Vercel Build API has this issue too, but worse since the Vercel Build Output API doesn't do chunking. Would love to get your thoughts on supporting other platforms and what good methods forward would be. Here's an example implementation https://nextflare-demo.pages.dev/ (source code is available on request). |
Beta Was this translation helpful? Give feedback.
-
Does the edge runtime supports post requests? I checked the docs and could not find any information that they could not |
Beta Was this translation helpful? Give feedback.
-
Just wanted to share feedback on this as well, as along with quite a few others in this thread, may have been under a different impression of the outcome here?
As I understand it, prior to this change, here was the current status of support for Node / Edge
Now after this change, we have this
And so I think what might have caught myself and others by surprise is that Middleware + Node is still 🚫 ? Are we missing something? All of my testing has concluded that Middleware in NodeJS is not supported. 😢 So would it be possible to get Middleware in Node.js at some point? 💯
|
Beta Was this translation helpful? Give feedback.
-
Currently blocked even experimenting with Next 13 app directory until middleware can run as non-edge. What's especially frustrating is that this is self-hosted anyway. Our middleware is running in Node regardless! Please let us remove this limitation 🙏🙏🙏 |
Beta Was this translation helpful? Give feedback.
-
Hey! Thank you all for great job! I tried to use edge runtime on vercel with
but after upgrading to next
but it also does not work. Please help me to use edge runtime for the whole project |
Beta Was this translation helpful? Give feedback.
-
Is this RFC dead? No node runtime on middleware at all? |
Beta Was this translation helpful? Give feedback.
-
I need a way to set the edge runtime to the whole project. Pages can be configured from a root layout, but API routes must be configured individually. |
Beta Was this translation helpful? Give feedback.
-
I see this doc now I guess middleware can not be used with nodejs runtime. |
Beta Was this translation helpful? Give feedback.
-
Imagine building a framework for building web apps with server side rendering that does not support running simple functions on every http request on the server (to handle cross cutting concerns like logging, tracing, auth, cookies, headers). This restriction that middleware only runs in the edge runtime is awful. I spend a lot of time configuring middleware to work with my auth logic just to find out that I can not use it with my simple node.js custom deployment. I dont want to be forced to ANY hosting provider, I want to choose myself where I want to host the server that runs my app. This is the problem with a profit-oriented company really locking the community to a particular deployment platform. AT LEAST stating that middleware can only run in edge runtime which is only really supported by vercel should be the first thing in the middleware documentation, so everybody knows what they sign up for. Middleware running in node.js alongside the other server code should be the default and running on the edge should be the opt-in. Not the other way around! And the other way is not even supported... Man I had a bad gut feeling with next. How this got 100k stars? Delusional. For all folks struggling with this and don't have the biggest performance requirements: Spin up a reverse proxy that handles your middleware stuff and route it to your actual pure node.js next.js server. And for folks who are not yet locked in: Really think about using something different than next :) |
Beta Was this translation helpful? Give feedback.
-
Motivation
Next.js has two server runtimes to run your application: the Node.js Runtime (default) and the Edge Runtime. When doing SSR, or serving API routes, the application code will be executed in the Node.js Runtime; and for Middleware, it will be running in the Edge Runtime.
There are many differences between these two runtimes. Here’s a quick comparison:
fetch
Next.js' default runtime configuration is good for most use cases, but there’re still many reasons to change to one runtime over the other one. For example, to enable React 18’s streaming SSR feature, you cannot deploy the app to lambda. For API routes that relies on native Node.js APIs, it has to be running in the Node.js Runtime. But if an API only uses a simple cookie-based authentication, using middleware and the Edge Runtime will be a better choice due to its lower latency as well as better scalability.
In a real-world application, it will be very helpful to have the ability to customize the runtime for each Next.js route, for both pages and API routes.
Terminologies Used in This RFC
Proposal
1. Global Runtime
A
runtime
option innext.config.js
, which can be eithernodejs
oredge
(or leave it asundefined
):The value stands for the preferred global runtime for Next.js to use for SSR (gSSP-based SSR, Suspense-based SSR), SSG and API routes. If the option is missing, Next.js will automatically choose the runtime based on the type of each page (see "What’s happening under the hood?" below).
This new option will not change any existing behavior if it's not set. Next.js will prefer the Node.js Runtime for corresponding routes. This ensures the best ecosystem interoperability and support for self-hosting3.
2. Per-route Runtime Config
For each route (pages and API routes), you can export an option
runtime
config such as:Note: this option must be static, so you can’t do things like
export const config = { runtime: 'node' + 'js' }
.Same as the global
runtime
option, the per-routeruntime
is optional, and can be set tonodejs
oredge
as well. This option determines the runtime for this specific route, that will be used by SSR (gSSP-based SSR, Suspense-based SSR), SSG and API routes.For a page route, if the
runtime
option is not set and there is no gSSP/gSP/gIP used, Next.js will do the automatic static optimization and there will be no server runtime involved.The detailed behavior is listed below.
What’s happening under the hood?
If a third-party library uses Suspense for data fetching on the server it must only use static data. Otherwise it should throw an error on the server which triggers client rendering of that Suspense boundary instead. That's because if you use Suspense on the server, there's no way to get the correct snapshot of the data to the client for hydration. Therefore third-party Suspense libraries must only be used on the client, use static data or be client-only. Therefore Next.js will assume that it's safe to do Automatic Static Optimization.
This table lists the target runtime of different pages, when configuring with different per-route
runtime
option:runtime
Configruntime: 'edge'
runtime: 'nodejs'
The global
runtime
config, when not specified, fallbacks to the Node.js Runtime. It prevents breaking changes from previous Next.js versions.FAQ
What happens to my existing application, if Next.js ships this feature?
Nothing will happen and the behavior stays unchanged if you don't configure any of these new options. This new feature is backward compatible.
Can we automatically detect the best runtime for each page, instead of configuring it manually?
We are looking into this direction but it's not currently possible. Next.js needs to know the target runtime before compiling your code. And another reason is that we want to keep it deterministic for the user, and the runtime won’t change unexpectedly due to code and dependency changes.
For React 18, why doesn’t Next.js enable streaming SSR for every page by default?
This conflicts with Automatic Static Optimization of Next.js. If we do that, all pages will be SSR’d in-flight instead of served as static HTML files generated at build time (since we can’t simply tell if a page uses Suspense or not). This would be a breaking change for all existing applications (no matter you are self-hosting Next.js or deploying it to any hosting platform).
The
runtime
config serves like a hint to tell Next.js that this page has a SSR runtime.I'm not using React 18 or React Server Components, why do I need this feature?
Even if React 18's new SSR architecture and Server Components are not used, you will still be able to customize the runtime for your gSSP and gSP functions as well as API routes.
I'm hosting Next.js in a Node.js server, can I use the Edge Runtime?
It will also be possible to use the Edge Runtime when self-hosting on a Node.js server. Using the Edge Runtime, even when self-hosted, provides isolation & simplicity benefits for deployment platforms like Kubernetes as well.
Footnotes
https://vercel.com/docs/concepts/functions/conceptual-model#cold-and-hot-boots ↩
https://github.com/reactwg/react-18/discussions/37 ↩
It will also be possible to use the Edge Runtime when self-hosting on a Node.js server. Using the Edge Runtime, even when self-hosted, provides security & simplicity benefits for deployment platforms like Kubernetes as well. ↩
Beta Was this translation helpful? Give feedback.
All reactions