Edge Functions run on the Edge Runtime, a runtime built on top of the V8 JavaScript engine. To learn more about the Edge Runtime and its supported APIs, see the Edge Runtime documentation.
The config
object is used to configure your Edge Function in Next.js pages
router and Edge Functions for non-Next.js frameworks.
- The
runtime
property is required, and must be set toedge
to run your function on the Edge Runtime - The
regions
property is optional and can be used to specify the region to which your function should be deployed. By default, your function will be deployed to all regions
export const config = {
runtime: 'edge',
regions: 'iad1', // optional
};
Property | Type | Description |
---|---|---|
runtime | string | The runtime to use. Must be set to edge |
regions | string | A single region to deploy your function to. Defaults to all regions when not set |
When using the app
router in Next.js, you must configure your Edge Function with segment options rather than a config
object.
- The
runtime
option is required, and must be set to'edge'
to run your function on the Edge Runtime - The
preferredRegion
option is optional and can be used to specify the region to which your function should be deployed. By default, your function will be deployed to all regions
export const runtime = 'edge';
export const dynamic = 'auto';
export const dynamicParams = true;
export const revalidate = false;
export const fetchCache = 'auto';
export const preferredRegion = 'auto';
export const maxDuration = 5;
export default function MyComponent() {}
Option | Type | Description |
---|---|---|
string | The runtime to use. Must be set to edge | |
string | A single region to deploy your function to. Defaults to all regions when not set | |
string | Change the dynamic behavior of a layout or page to fully static or fully dynamic. | |
boolean | Control what happens when a dynamic segment is visited that was not generated with generateStaticParams. | |
string , boolean , number | Set the default revalidation time for a layout or page. | |
string | fetchCache allows you to override the default cache option of all fetch requests in a layout or page. | |
number | Based on your deployment platform, you may be able to use a higher default execution time for your function. |
The Edge Functions signature is made up of two parameters: request
and context
. The request
parameter is an instance of the Request object, and the context
parameter is an object containing the waitUntil
method.
Parameter | Description | Next.js | Other Frameworks |
---|---|---|---|
request | An instance of the Request object | ||
context | An extension to the standard Request object |
export const runtime = 'edge';
export function GET(request: Request) {
return Response.json({
name: `Hello, from ${request.url} I'm an Edge Function!`,
});
}
The Request
object represents an HTTP request. It is a wrapper around the Fetch API Request
object.
Property | Type | Description |
---|---|---|
url | string | The URL of the request |
method | string | The HTTP method of the request |
headers | Headers | The headers of the request |
body | The body of the request | |
bodyUsed | boolean | Whether the body has been read |
cache | string | The cache mode of the request |
credentials | string | The credentials mode of the request |
destination | string | The destination of the request |
integrity | string | The integrity of the request |
redirect | string | The redirect mode of the request |
referrer | string | The referrer of the request |
referrerPolicy | string | The referrer policy of the request |
mode | string | The mode of the request |
signal | The signal of the request | |
arrayBuffer | function | Returns a promise that resolves with an ArrayBuffer |
blob | function | Returns a promise that resolves with a Blob |
formData | function | Returns a promise that resolves with a FormData |
json | function | Returns a promise that resolves with a JSON object |
text | function | Returns a promise that resolves with a string |
clone | function | Returns a clone of the request |
To learn more about the NextRequest
object and its properties, visit the Next.js documentation.
The waitUntil()
method is not currently supported with app-router in Next.js. Vercel is working to implement this functionality.
Property | Type | Description |
---|---|---|
(promise: Promise<unknown>): void | Prolongs the execution of the function until the promise passed to waitUntil is resolved |
Learn more about Edge Functions by exploring the following resources:
Was this helpful?