Skip to content

Edge Runtime

Edge Runtime用于中间件,并支持以下API:

网络API

API描述
Blob表示一个blob
fetch获取资源
FetchEvent表示一个获取事件
File表示一个文件
FormData表示表单数据
Headers表示HTTP头
Request表示一个HTTP请求
Response表示一个HTTP响应
URLSearchParams表示URL搜索参数
WebSocket表示一个websocket连接

编码API

API描述
atob解码base-64编码的字符串
btoa将字符串编码为base-64
TextDecoder将Uint8Array解码为字符串
TextDecoderStream可链接的流解码器
TextEncoder将字符串编码为Uint8Array
TextEncoderStream可链接的流编码器

Stream APIs

API描述
ReadableStream表示一个可读流
ReadableStreamBYOBReader表示一个ReadableStream的读取器
ReadableStreamDefaultReader表示一个ReadableStream的读取器
TransformStream表示一个转换流
WritableStream表示一个可写流
WritableStreamDefaultWriter表示一个WritableStream的写入器

Crypto APIs

API描述
crypto提供对平台加密功能的访问
CryptoKey表示一个加密密钥
SubtleCrypto提供对常见加密原语的访问,如哈希、签名、加密或解密

Web Standard APIs

APIDescription
AbortControllerAllows you to abort one or more DOM requests as and when desired
ArrayRepresents an array of values
ArrayBufferRepresents a generic, fixed-length raw binary data buffer
AtomicsProvides atomic operations as static methods
BigIntRepresents a whole number with arbitrary precision
BigInt64ArrayRepresents a typed array of 64-bit signed integers
BigUint64ArrayRepresents a typed array of 64-bit unsigned integers
BooleanRepresents a logical entity and can have two values: true and false
clearIntervalCancels a timed, repeating action which was previously established by a call to setInterval()
clearTimeoutCancels a timed, repeating action which was previously established by a call to setTimeout()
consoleProvides access to the browser's debugging console
DataViewRepresents a generic view of an ArrayBuffer
DateRepresents a single moment in time in a platform-independent format
decodeURIDecodes a Uniform Resource Identifier (URI) previously created by encodeURI or by a similar routine
decodeURIComponentDecodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine
DOMExceptionRepresents an error that occurs in the DOM
encodeURIEncodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character
encodeURIComponentEncodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character
ErrorRepresents an error when trying to execute a statement or accessing a property
EvalErrorRepresents an error that occurs regarding the global function eval()
Float32ArrayRepresents a typed array of 32-bit floating point numbers
Float64ArrayRepresents a typed array of 64-bit floating point numbers
FunctionRepresents a function
InfinityRepresents the mathematical Infinity value
Int8ArrayRepresents a typed array of 8-bit signed integers
Int16ArrayRepresents a typed array of 16-bit signed integers
Int32ArrayRepresents a typed array of 32-bit signed integers
IntlProvides access to internationalization and localization functionality
isFiniteDetermines whether a value is a finite number
isNaNDetermines whether a value is NaN or not
JSONProvides functionality to convert JavaScript values to and from the JSON format
MapRepresents a collection of values, where each value may occur only once
MathProvides access to mathematical functions and constants
NumberRepresents a numeric value
ObjectRepresents the object that is the base of all JavaScript objects
parseFloatParses a string argument and returns a floating point number
parseIntParses a string argument and returns an integer of the specified radix
PromiseRepresents the eventual completion (or failure) of an asynchronous operation, and its resulting value
ProxyRepresents an object that is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc)
queueMicrotaskQueues a microtask to be executed
RangeErrorRepresents an error when a value is not in the set or range of allowed values
ReferenceErrorRepresents an error when a non-existent variable is referenced
ReflectProvides methods for interceptable JavaScript operations
RegExpRepresents a regular expression, allowing you to match combinations of characters
SetRepresents a collection of values, where each value may occur only once
setIntervalRepeatedly calls a function, with a fixed time delay between each call
setTimeoutCalls a function or evaluates an expression after a specified number of milliseconds
SharedArrayBufferRepresents a generic, fixed-length raw binary data buffer
StringRepresents a sequence of characters
structuredCloneCreates a deep copy of a value
SymbolRepresents a unique and immutable data type that is used as the key of an object property
SyntaxErrorRepresents an error when trying to interpret syntactically invalid code
TypeErrorRepresents an error when a value is not of the expected type
Uint8ArrayRepresents a typed array of 8-bit unsigned integers
Uint8ClampedArrayRepresents a typed array of 8-bit unsigned integers clamped to 0-255
Uint32ArrayRepresents a typed array of 32-bit unsigned integers
URIErrorRepresents an error when a global URI handling function was used in a wrong way
URLRepresents an object providing static methods used for creating object URLs
URLPatternRepresents a URL pattern
URLSearchParamsRepresents a collection of key/value pairs
WeakMapRepresents a collection of key/value pairs in which the keys are weakly referenced
WeakSetRepresents a collection of objects in which each object may occur only once
WebAssemblyProvides access to WebAssembly

Next.js 特定 Polyfills

环境变量

你可以使用 process.env 来访问 环境变量,这适用于 next devnext build

不支持的 API

Edge 运行时有一些限制,包括:

  • 原生 Node.js API 不被支持。例如,你不能读取或写入文件系统。
  • node_modules 可以使用,只要它们实现了 ES 模块并不使用原生 Node.js API。
  • 直接调用 require不被允许 的。请改用 ES 模块。

以下 JavaScript 语言特性被禁用,将无法工作

API描述
eval评估表示为字符串的 JavaScript 代码
new Function(evalString)使用作为参数提供的代码创建一个新的函数
WebAssembly.compile从缓冲区源编译 WebAssembly 模块
WebAssembly.instantiate从缓冲区源编译并实例化 WebAssembly 模块

在极少数情况下,你的代码可能包含(或导入)一些动态代码评估语句,这些语句在运行时无法到达,并且不能通过摇树(tree shaking)移除。 你可以通过中间件配置放宽检查,以允许特定文件:

javascript
export const config = {
  unstable_allowDynamic: [
    // 允许单个文件
    '/lib/utilities.js',
    // 使用 glob 允许 function-bind 第三方模块中的任何内容
    '/node_modules/function-bind/**',
  ],
}

unstable_allowDynamic 是一个 glob,或 glob 数组,忽略特定文件的动态代码评估。这些 glob 相对于你的应用程序根文件夹。

须知,如果这些语句在 Edge 上执行,它们将抛出异常并导致运行时错误