diff --git a/docs/api-reference/edge-runtime.md b/docs/api-reference/edge-runtime.md index c62a84973bf..6d0b7f0c839 100644 --- a/docs/api-reference/edge-runtime.md +++ b/docs/api-reference/edge-runtime.md @@ -136,6 +136,25 @@ The following JavaScript language features are disabled, and **will not work:** - `eval`: Evaluates JavaScript code represented as a string - `new Function(evalString)`: Creates a new function with the code provided as an argument +- `WebAssembly.compile` +- `WebAssembly.instantiate` with [a buffer parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate#primary_overload_%E2%80%94_taking_wasm_binary_code) + +In rare cases, your code could contain (or import) some dynamic code evaluation statements which _can not be reached at runtime_ and which can not be removed by threeshaking. +You can relax the check to allow specific files with your Middleware or Edge API Route exported configuration: + +```javascript +export const config = { + runtime: 'experimental-edge', // for Edge API Routes only + allowDynamic: [ + '/lib/utilities.js', // allows a single file + '/node_modules/function-bind/**', // use a glob to allow anything in the function-bind 3rd party module + ], +} +``` + +`allowDynamic` is a [glob](https://github.com/micromatch/micromatch#matching-features), or an array of globs, ignoring dynamic code evaluation for specific files. The globs are relative to your application root folder. + +Be warned that if these statements are executed on the Edge, _they will throw and fail your route_. ## Related diff --git a/errors/edge-dynamic-code-evaluation.md b/errors/edge-dynamic-code-evaluation.md new file mode 100644 index 00000000000..a97dd0e85e8 --- /dev/null +++ b/errors/edge-dynamic-code-evaluation.md @@ -0,0 +1,34 @@ +# Dynamic code evaluation is not available in Middlewares or Edge API Routes + +#### Why This Error Occurred + +`eval()`, `new Function()` or compiling WASM binaries dynamically is not allowed in Middlewares or Edge API Routes. +Specifically, the following APIs are not supported: + +- `eval()` +- `new Function()` +- `WebAssembly.compile` +- `WebAssembly.instantiate` with [a buffer parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate#primary_overload_%E2%80%94_taking_wasm_binary_code) + +#### Possible Ways to Fix It + +You can bundle your WASM binaries using `import`: + +```typescript +import { NextResponse } from 'next/server' +import squareWasm from './square.wasm?module' + +export default async function middleware() { + const m = await WebAssembly.instantiate(squareWasm) + const answer = m.exports.square(9) + + const response = NextResponse.next() + response.headers.set('x-square', answer.toString()) + return response +} +``` + +In rare cases, your code could contain (or import) some dynamic code evaluation statements which _can not be reached at runtime_ and which can not be removed by threeshaking. +You can relax the check to allow specific files with your Middleware or Edge API Route exported [configuration](https://nextjs.org/docs/api-reference/edge-runtime#unsupported-apis). + +Be warned that if these statements are executed on the Edge, _they will throw and fail your route_. diff --git a/errors/manifest.json b/errors/manifest.json index 3f013867de5..25a43afd1ba 100644 --- a/errors/manifest.json +++ b/errors/manifest.json @@ -712,7 +712,11 @@ }, { "title": "middleware-dynamic-wasm-compilation", - "path": "/errors/middleware-dynamic-wasm-compilation.md" + "path": "/errors/edge-dynamic-code-evaluation.md" + }, + { + "title": "edge-dynamic-code-evaluation", + "path": "/errors/edge-dynamic-code-evaluation.md" }, { "title": "node-module-in-edge-runtime", diff --git a/errors/middleware-dynamic-wasm-compilation.md b/errors/middleware-dynamic-wasm-compilation.md deleted file mode 100644 index 7b5272a41d5..00000000000 --- a/errors/middleware-dynamic-wasm-compilation.md +++ /dev/null @@ -1,27 +0,0 @@ -# Dynamic WASM compilation is not available in Middlewares - -#### Why This Error Occurred - -Compiling WASM binaries dynamically is not allowed in Middlewares. Specifically, -the following APIs are not supported: - -- `WebAssembly.compile` -- `WebAssembly.instantiate` with [a buffer parameter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate#primary_overload_%E2%80%94_taking_wasm_binary_code) - -#### Possible Ways to Fix It - -Bundle your WASM binaries using `import`: - -```typescript -import { NextResponse } from 'next/server' -import squareWasm from './square.wasm?module' - -export default async function middleware() { - const m = await WebAssembly.instantiate(squareWasm) - const answer = m.exports.square(9) - - const response = NextResponse.next() - response.headers.set('x-square', answer.toString()) - return response -} -``` diff --git a/packages/next/server/web/sandbox/context.ts b/packages/next/server/web/sandbox/context.ts index 65b076daa3a..d905ddc7625 100644 --- a/packages/next/server/web/sandbox/context.ts +++ b/packages/next/server/web/sandbox/context.ts @@ -148,7 +148,8 @@ async function createModuleContext(options: ModuleContextOptions) { if (!warnedEvals.has(key)) { const warning = getServerError( new Error( - `Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime` + `Dynamic Code Evaluation (e. g. 'eval', 'new Function') not allowed in Edge Runtime +Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation` ), COMPILER_NAMES.edgeServer ) @@ -166,7 +167,7 @@ async function createModuleContext(options: ModuleContextOptions) { if (!warnedWasmCodegens.has(key)) { const warning = getServerError( new Error(`Dynamic WASM code generation (e. g. 'WebAssembly.compile') not allowed in Edge Runtime. -Learn More: https://nextjs.org/docs/messages/middleware-dynamic-wasm-compilation`), +Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`), COMPILER_NAMES.edgeServer ) warning.name = 'DynamicWasmCodeGenerationWarning' @@ -193,7 +194,7 @@ Learn More: https://nextjs.org/docs/messages/middleware-dynamic-wasm-compilation if (instantiatedFromBuffer && !warnedWasmCodegens.has(key)) { const warning = getServerError( new Error(`Dynamic WASM code generation ('WebAssembly.instantiate' with a buffer parameter) not allowed in Edge Runtime. -Learn More: https://nextjs.org/docs/messages/middleware-dynamic-wasm-compilation`), +Learn More: https://nextjs.org/docs/messages/edge-dynamic-code-evaluation`), COMPILER_NAMES.edgeServer ) warning.name = 'DynamicWasmCodeGenerationWarning'