Does Vercel run next.js server components in a serverless fashion (i.e. do they spin up a long-lived or short-lived node.js server)? #61913
Replies: 2 comments 2 replies
-
Relevant: It is vaguely documented, but here is some initial evidence that when any server component is rendered on the server by Vercel, it is done so in a serverless fashion (i.e. that it does not keep a long-lasting node.js server running): https://vercel.com/docs/frameworks/nextjs#incremental-static-regeneration As seen, the docs page groups server-rendering with Route Handlers (referred to by its old name "API Routes"), calling it all Vercel Functions, which is serverless (meaning the node.js server it spins up only runs for the duration of the function it executes). In other words, I am not 100% certain that server components are run in one-off node.js servers by Vercel (i.e. serverless), but it is my current best guess. |
Beta Was this translation helpful? Give feedback.
-
On Vercel they are serverless functions based on the Vercel docs:
Although the self-hosted version seems to be a long-running Node server: |
Beta Was this translation helpful? Give feedback.
-
I have been using Next.js for a while and realized I am missing a core piece of understanding relating to how the framework actually operates under the hood, as it relates to server-side code.
Background
Next.js has three categories of code that runs server side (as far as I know):
Code in Server Components (e.g. a normal async page component). It is possible to fetch external data in these server components. The code will never be executed by the browser.
Code in Route Handlers (previously called API Routes). This code is executed when a machine (typically client side code in the browser) sends a request to the handler's URL. Route Handlers are executed by app host (e.g. Vercel) on demand, in a node.js server environment. That server is started on-demand, whenever the function runs, thus there is a small lag to run these functions. This way of running functions is called "serverless", because we do not need to run and manage our own server to execute them. Vercel, for instance, can execute these from a central server (e.g. in the US), or from one of many servers around the world (called the edge) that runs smaller and faster versions of the node.js runtime (called the edge runtime).
Finally, Server Actions also execute server-side, in a node.js environment. They are similar to Route Handlers, in that they are "serverless" (called by client-side code in the browser, resulting in a server booting up to run the code and then shutting down).
As I was looking into Prisma, I read that the Prisma Client either establishes long-lived connections to a database, or short-lived connections. See: https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/databases-connections
Question
It is clear to me that Route Handlers and Server Actions will spawn short-lived node.js servers and thus create a new Prisma Client object per function call. As such, these Prisma database connections are short-lived, meaning the Prisma Client object only lives for the duration of the function call.
Firstly, is my above understanding correct?
Secondly, if we contact our database in a normal server component in a dynamic route (i.e. SSR, not SSG), will that be a short-lived node.js execution, or does that node.js environment persist / live over time (so Prisma Client objects are actually long-lived in that scenario)?
Edit:
Vercel caches data fetches (in Data Cache), so the database requests might not actually run if the same data is requested again. The question above disregards the cache, and is related to code execution and creation of the Prisma Client object and whether its database connection is long-lived or short-lived.
In static routes, bundle (react server component payload, and html/css/js bundle) resulting from execution of react server component, is cached in Full Route Cache on e.g. Vercel. This does not happen with dynamic routes.
Beta Was this translation helpful? Give feedback.
All reactions