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
API | Description |
---|---|
AbortController | Allows you to abort one or more DOM requests as and when desired |
Array | Represents an array of values |
ArrayBuffer | Represents a generic, fixed-length raw binary data buffer |
Atomics | Provides atomic operations as static methods |
BigInt | Represents a whole number with arbitrary precision |
BigInt64Array | Represents a typed array of 64-bit signed integers |
BigUint64Array | Represents a typed array of 64-bit unsigned integers |
Boolean | Represents a logical entity and can have two values: true and false |
clearInterval | Cancels a timed, repeating action which was previously established by a call to setInterval() |
clearTimeout | Cancels a timed, repeating action which was previously established by a call to setTimeout() |
console | Provides access to the browser's debugging console |
DataView | Represents a generic view of an ArrayBuffer |
Date | Represents a single moment in time in a platform-independent format |
decodeURI | Decodes a Uniform Resource Identifier (URI) previously created by encodeURI or by a similar routine |
decodeURIComponent | Decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine |
DOMException | Represents an error that occurs in the DOM |
encodeURI | Encodes 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 |
encodeURIComponent | Encodes 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 |
Error | Represents an error when trying to execute a statement or accessing a property |
EvalError | Represents an error that occurs regarding the global function eval() |
Float32Array | Represents a typed array of 32-bit floating point numbers |
Float64Array | Represents a typed array of 64-bit floating point numbers |
Function | Represents a function |
Infinity | Represents the mathematical Infinity value |
Int8Array | Represents a typed array of 8-bit signed integers |
Int16Array | Represents a typed array of 16-bit signed integers |
Int32Array | Represents a typed array of 32-bit signed integers |
Intl | Provides access to internationalization and localization functionality |
isFinite | Determines whether a value is a finite number |
isNaN | Determines whether a value is NaN or not |
JSON | Provides functionality to convert JavaScript values to and from the JSON format |
Map | Represents a collection of values, where each value may occur only once |
Math | Provides access to mathematical functions and constants |
Number | Represents a numeric value |
Object | Represents the object that is the base of all JavaScript objects |
parseFloat | Parses a string argument and returns a floating point number |
parseInt | Parses a string argument and returns an integer of the specified radix |
Promise | Represents the eventual completion (or failure) of an asynchronous operation, and its resulting value |
Proxy | Represents an object that is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc) |
queueMicrotask | Queues a microtask to be executed |
RangeError | Represents an error when a value is not in the set or range of allowed values |
ReferenceError | Represents an error when a non-existent variable is referenced |
Reflect | Provides methods for interceptable JavaScript operations |
RegExp | Represents a regular expression, allowing you to match combinations of characters |
Set | Represents a collection of values, where each value may occur only once |
setInterval | Repeatedly calls a function, with a fixed time delay between each call |
setTimeout | Calls a function or evaluates an expression after a specified number of milliseconds |
SharedArrayBuffer | Represents a generic, fixed-length raw binary data buffer |
String | Represents a sequence of characters |
structuredClone | Creates a deep copy of a value |
Symbol | Represents a unique and immutable data type that is used as the key of an object property |
SyntaxError | Represents an error when trying to interpret syntactically invalid code |
TypeError | Represents an error when a value is not of the expected type |
Uint8Array | Represents a typed array of 8-bit unsigned integers |
Uint8ClampedArray | Represents a typed array of 8-bit unsigned integers clamped to 0-255 |
Uint32Array | Represents a typed array of 32-bit unsigned integers |
URIError | Represents an error when a global URI handling function was used in a wrong way |
URL | Represents an object providing static methods used for creating object URLs |
URLPattern | Represents a URL pattern |
URLSearchParams | Represents a collection of key/value pairs |
WeakMap | Represents a collection of key/value pairs in which the keys are weakly referenced |
WeakSet | Represents a collection of objects in which each object may occur only once |
WebAssembly | Provides access to WebAssembly |
Next.js 特定 Polyfills
环境变量
你可以使用 process.env
来访问 环境变量,这适用于 next dev
和 next 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 上执行,它们将抛出异常并导致运行时错误。