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. The runtime
property is required, and must be set to edge
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 |
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 |
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 |
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 can be used to keep the function running after a response has been sent. This is useful when you have an async task that you want to keep running after returning a response.
The example below will send a response immediately, but will keep the function running for ten seconds, fetch an album and log it to the console.
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?