Appearance
Edge Runtime
The Next.js Edge Runtime is based on standard Web APIs, which is used by Middleware.
Runtime APIs
Globals
Base64
atob
: Decodes a string of data which has been encoded using base-64 encodingbtoa
: Creates a base-64 encoded ASCII string from a string of binary data
Encoding
TextEncoder
: Takes a stream of code points as input and emits a stream of bytes (UTF8)TextDecoder
: Takes a stream of bytes as input and emit a stream of code points
Environment
p
: Holds an object with all environment variables for bothrocess.env next dev
andnext build
in the exact same way as any other page or API in Next.js
Fetch
The Web Fetch API can be used from the runtime, enabling you to use Middleware as a proxy, or connect to external storage APIs
A potential caveat to using the Fetch API in a Middleware function is latency. For example, if you have a Middleware function running a fetch request to New York, and a user accesses your site from London, the request will be resolved from the nearest Edge to the user (in this case, London), to the origin of the request, New York. There is a risk this could happen on every request, making your site slow to respond. When using the Fetch API, you must make sure it does not run on every single request made.
Streams
TransformStream
: Consists of a pair of streams: a writable stream known as its writable side, and a readable stream, known as its readable side. Writes to the writable side, result in new data being made available for reading from the readable side. Support for web streams is quite limited at the moment, although it is more extended in the development environmentReadableStream
: A readable stream of byte dataWritableStream
: A standard abstraction for writing streaming data to a destination, known as a sink
Timers
setInterval
: Schedules a function to execute every time a given number of milliseconds elapsesclearInterval
: Cancels the repeated execution set usingsetInterval()
setTimeout
: Schedules a function to execute in a given amount of timeclearTimeout
: Cancels the delayed execution set usingsetTimeout()
Web
Headers
: A WHATWG implementation of the headers APIURL
: A WHATWG implementation of the URL API.URLSearchParams
: A WHATWG implementation ofURLSearchParams
Crypto
Crypto
: TheCrypto
interface represents basic cryptography features available in the current contextcrypto.randomUUID
: Lets you generate a v4 UUID using a cryptographically secure random number generatorcrypto.getRandomValues
: Lets you get cryptographically strong random valuescrypto.subtle
: A read-only property that returns a SubtleCrypto which can then be used to perform low-level cryptographic operations
Logging
console.debug
: Outputs a message to the console with the log level debugconsole.info
: Informative logging of information. You may use string substitution and additional arguments with this methodconsole.clear
: Clears the consoleconsole.dir
: Displays an interactive listing of the properties of a specified JavaScript objectconsole.count
: Log the number of times this line has been called with the given labelconsole.time
: Starts a timer with a name specified as an input parameter
Unsupported APIs
The Edge Runtime has some restrictions including:
- Native Node.js APIs are not supported. For example, you can't read or write to the filesystem
- Node Modules can be used, as long as they implement ES Modules and do not use any native Node.js APIs
- Calling
require
directly is not allowed. Use ES Modules instead
The following JavaScript language features are disabled, and will not work:
eval
: Evaluates JavaScript code represented as a stringnew Function(evalString)
: Creates a new function with the code provided as an argument
Related
MiddlewareRun code before a request is completed.
Middleware API ReferenceLearn more about the supported APIs for Middleware.