|
1 | 1 | --- |
2 | | -title: Edge and Node.js Runtimes |
| 2 | +title: Runtimes |
3 | 3 | description: Learn about the switchable runtimes (Edge and Node.js) in Next.js. |
| 4 | +related: |
| 5 | + description: View the Edge Runtime API reference. |
| 6 | + links: |
| 7 | + - app/api-reference/edge |
4 | 8 | --- |
5 | 9 |
|
6 | 10 | {/* The content of this doc is shared between the app and pages router. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */} |
7 | 11 |
|
8 | | -In the context of Next.js, runtime refers to the set of libraries, APIs, and general functionality available to your code during execution. |
| 12 | +Next.js has two server runtimes you can use in your application: |
9 | 13 |
|
10 | | -On the server, there are two runtimes where parts of your application code can be rendered: |
| 14 | +- The **Node.js Runtime** (default) which has access to all Node.js APIs and compatible packages from the ecosystem. |
| 15 | +- The **Edge Runtime** which contains a more limited [set of APIs](/docs/app/api-reference/edge). |
11 | 16 |
|
12 | | -- The **Node.js Runtime** (default) has access to all Node.js APIs and compatible packages from the ecosystem. |
13 | | -- The **Edge Runtime** is based on [Web APIs](/docs/app/api-reference/edge). |
| 17 | +## Use Cases |
14 | 18 |
|
15 | | -## Runtime Differences |
| 19 | +- The Node.js runtime is used for rendering your application. |
| 20 | +- The Edge runtime is used for Middleware (routing rules like redirects, rewrites, and setting headers). |
16 | 21 |
|
17 | | -There are many considerations to make when choosing a runtime. This table shows the major differences at a glance. If you want a more in-depth analysis of the differences, check out the sections below. |
| 22 | +## Caveats |
18 | 23 |
|
19 | | -| | Node | Serverless | Edge | |
20 | | -| ------------------------------------------------------------------------------------------------------------------------------------- | ------ | ---------- | ---------------- | |
21 | | -| Cold Boot | / | Normal | Low | |
22 | | -| [HTTP Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming) | Yes | Yes | Yes | |
23 | | -| IO | All | All | `fetch` | |
24 | | -| Scalability | / | High | Highest | |
25 | | -| Security | Normal | High | High | |
26 | | -| Latency | Normal | Low | Lowest | |
27 | | -| npm Packages | All | All | A smaller subset | |
28 | | -| [Static Rendering](/docs/app/building-your-application/rendering/server-components#static-rendering-default) | Yes | Yes | No | |
29 | | -| [Dynamic Rendering](/docs/app/building-your-application/rendering/server-components#dynamic-rendering) | Yes | Yes | Yes | |
30 | | -| [Data Revalidation w/ `fetch`](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data) | Yes | Yes | Yes | |
31 | | - |
32 | | -### Edge Runtime |
33 | | - |
34 | | -In Next.js, the lightweight Edge Runtime is a subset of available Node.js APIs. |
35 | | - |
36 | | -The Edge Runtime is ideal if you need to deliver dynamic, personalized content at low latency with small, simple functions. The Edge Runtime's speed comes from its minimal use of resources, but that can be limiting in many scenarios. |
37 | | - |
38 | | -For example, code executed in the Edge Runtime [on Vercel cannot exceed between 1 MB and 4 MB](https://vercel.com/docs/concepts/limits/overview#edge-middleware-and-edge-functions-size), this limit includes imported packages, fonts and files, and will vary depending on your deployment infrastructure. In addition, the Edge Runtime does not support all Node.js APIs meaning some `npm` packages may not work. For example, "Module not found: Can't resolve 'fs'" or similar errors. We recommend using the Node.js runtime if you need to use these APIs or packages. |
39 | | - |
40 | | -### Node.js Runtime |
41 | | - |
42 | | -Using the Node.js runtime gives you access to all Node.js APIs, and all npm packages that rely on them. However, it's not as fast to start up as routes using the Edge runtime. |
43 | | - |
44 | | -Deploying your Next.js application to a Node.js server will require managing, scaling, and configuring your infrastructure. Alternatively, you can consider deploying your Next.js application to a serverless platform like Vercel, which will handle this for you. |
45 | | - |
46 | | -### Serverless Node.js |
47 | | - |
48 | | -Serverless is ideal if you need a scalable solution that can handle more complex computational loads than the Edge Runtime. With Serverless Functions on Vercel, for example, your overall code size is [50MB](https://vercel.com/docs/concepts/limits/overview#serverless-function-size) including imported packages, fonts, and files. |
49 | | - |
50 | | -The downside compared to routes using the [Edge](https://vercel.com/docs/concepts/functions/edge-functions) is that it can take hundreds of milliseconds for Serverless Functions to boot up before they begin processing requests. Depending on the amount of traffic your site receives, this could be a frequent occurrence as the functions are not frequently "warm". |
51 | | - |
52 | | -<AppOnly> |
53 | | - |
54 | | -## Examples |
55 | | - |
56 | | -### Segment Runtime Option |
57 | | - |
58 | | -You can specify a runtime for individual route segments in your Next.js application. To do so, [declare a variable called `runtime` and export it](/docs/app/api-reference/file-conventions/route-segment-config). The variable must be a string, and must have a value of either `'nodejs'` or `'edge'` runtime. |
59 | | - |
60 | | -The following example demonstrates a page route segment that exports a `runtime` with a value of `'edge'`: |
61 | | - |
62 | | -```tsx filename="app/page.tsx" switcher |
63 | | -export const runtime = 'edge' // 'nodejs' (default) | 'edge' |
64 | | -``` |
65 | | - |
66 | | -```jsx filename="app/page.js" switcher |
67 | | -export const runtime = 'edge' // 'nodejs' (default) | 'edge' |
68 | | -``` |
69 | | - |
70 | | -You can also define `runtime` on a layout level, which will make all routes under the layout run on the edge runtime: |
71 | | - |
72 | | -```tsx filename="app/layout.tsx" switcher |
73 | | -export const runtime = 'edge' // 'nodejs' (default) | 'edge' |
74 | | -``` |
75 | | - |
76 | | -```jsx filename="app/layout.js" switcher |
77 | | -export const runtime = 'edge' // 'nodejs' (default) | 'edge' |
78 | | -``` |
79 | | - |
80 | | -If the segment runtime is _not_ set, the default `nodejs` runtime will be used. You do not need to use the `runtime` option if you do not plan to change from the Node.js runtime. |
81 | | - |
82 | | -</AppOnly> |
83 | | - |
84 | | -> Please refer to the [Node.js Docs](https://nodejs.org/docs/latest/api/) and [Edge Docs](/docs/app/api-reference/edge) for the full list of available APIs. Both runtimes can also support [streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming) depending on your deployment infrastructure. |
| 24 | +- The Edge runtime does not support all Node.js APIs. Some packages will not work. Learn more about the unsupported APIs in the [Edge Runtime](/docs/app/api-reference/edge#unsupported-apis). |
| 25 | +- The Edge runtime does not support Incremental Static Regeneration (ISR). |
| 26 | +- Both runtimes can support [streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming) depending on your deployment infrastructure. |
0 commit comments