Edge Runtime
We recommend migrating from edge to Node.js for improved performance and reliability. Both runtimes run on Fluid compute with Active CPU pricing.
To convert your Vercel Function to use the Edge runtime, add the following code to your function:
If you're not using a framework, you must either add
"type": "module" to your
package.json or change your JavaScript Functions'
file extensions from .js to
.mjs
By default, Vercel Functions using the Edge runtime execute in the region closest to the incoming request. You can set one or more preferred regions using the route segment config or specify a key within a config object to set one or more regions for your functions to execute in.
If your function depends on a data source, you may want it to be close to that source for fast responses.
To configure which region (or multiple regions) you want your function to execute in, pass the ID of your preferred region(s) in the following way:
If you're not using a framework, you must either add
"type": "module" to your
package.json or change your JavaScript Functions'
file extensions from .js to
.mjs
In the event of regional downtime, Vercel will automatically reroute traffic to the next closest CDN region on all plans. For more information on which regions Vercel routes traffic to, see Outage Resiliency.
Vercel Functions using the Edge runtime must begin sending a response within 25 seconds to maintain streaming capabilities beyond this period, and can continue streaming data for up to 300 seconds.
Vercel automatically scales your functions to handle traffic surges, ensuring optimal performance during increased loads. For more information, see Concurrency scaling.
The Edge runtime is built on top of the V8 engine, allowing it to run in isolated execution environments that don't require a container or virtual machine.
The Edge runtime provides a subset of Web APIs such as , , and .
The following tables list the APIs that are available in the Edge runtime.
| API | Description |
|---|---|
| Encodes a string into a Uint8Array | |
| Decodes a Uint8Array into a string | |
| Decodes a base-64 encoded string | |
| Encodes a string in base-64 |
You can check if your function is running on the Edge runtime by checking the global property. This can be helpful if you need to validate that your function is running on the Edge runtime in tests, or if you need to use a different API depending on the runtime.
The following modules can be imported with and without the prefix when using the statement:
| Module | Description |
|---|---|
| Manage asynchronous resources lifecycles with . Supports the WinterCG subset of APIs | |
| Facilitate event-driven programming with custom event emitters and listeners. This API is fully supported | |
| Efficiently manipulate binary data using fixed-size, raw memory allocations with . Every primitive compatible with accepts too | |
| Provide a set of assertion functions for verifying invariants in your code | |
| Offer various utility functions where we include / and |
Also, is globally exposed to maximize compatibility with existing Node.js modules.
The Edge runtime has some restrictions including:
- Some Node.js APIs other than the ones listed above are not supported. For example, you can't read or write to the filesystem
- can be used, as long as they implement ES Modules and do not use native Node.js APIs
- Calling directly is not allowed. Use instead
The following JavaScript language features are disabled, and will not work:
While is supported in Edge Runtime, it requires the Wasm source code to be provided using the import statement. This means you cannot use a buffer or byte array to dynamically compile the module at runtime.
You can use to access Environment Variables.
Middleware with the runtime configured is neither a Node.js nor browser application, which means it doesn't have access to all browser and Node.js APIs. Currently, our runtime offers a subset of browser APIs and some Node.js APIs and we plan to implement more functionality in the future.
In summary:
- Use ES modules
- Most libraries that use Node.js APIs as dependencies can't be used in Middleware with the runtime configured.
- Dynamic code execution (such as ) is not allowed (see the next section for more details)
Dynamic code execution is not available in Middleware with the runtime configured for security reasons. For example, the following APIs cannot be used:
| API | Description |
|---|---|
| Evaluates JavaScript code represented as a string | |
| Creates a new function with the code provided as an argument | |
| Compiles and instantiates a WebAssembly module from a buffer source |
You need to make sure libraries used in your Middleware with the runtime configured don't rely on dynamic code execution because it leads to a runtime error.
Middleware with the runtime configured must begin sending a response within 25 seconds.
You may continue streaming a response beyond that time and you can continue with asynchronous workloads in the background, after returning the response.
| Plan | Limit (after gzip compression) |
|---|---|
| Hobby | 1 MB |
| Pro | 2 MB |
| Enterprise | 4 MB |
The maximum size for an Vercel Function using the Edge runtime includes your JavaScript code, imported libraries and files (such as fonts), and all files bundled in the function.
If you reach the limit, make sure the code you are importing in your function is used and is not too heavy. You can use a package size checker tool like bundle to check the size of a package and search for a smaller alternative.
Environment Variables can be accessed through the object. Since JavaScript objects have methods to allow some operations on them, there are limitations on the names of Environment Variables to avoid having ambiguous code.
The following names will be ignored as Environment Variables to avoid overriding the object prototype:
Therefore, your code will always be able to use them with their expected behavior:
Was this helpful?