Skip to content

Commit

Permalink
docs(middleware): single error page for dynamic errors, documents con…
Browse files Browse the repository at this point in the history
…fig.allowDynamic key.
  • Loading branch information
feugy committed Sep 1, 2022
1 parent 093cdfd commit c762f83
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 31 deletions.
19 changes: 19 additions & 0 deletions docs/api-reference/edge-runtime.md
Expand Up @@ -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

Expand Down
34 changes: 34 additions & 0 deletions 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_.
6 changes: 5 additions & 1 deletion errors/manifest.json
Expand Up @@ -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",
Expand Down
27 changes: 0 additions & 27 deletions errors/middleware-dynamic-wasm-compilation.md

This file was deleted.

7 changes: 4 additions & 3 deletions packages/next/server/web/sandbox/context.ts
Expand Up @@ -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
)
Expand All @@ -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'
Expand All @@ -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'
Expand Down

0 comments on commit c762f83

Please sign in to comment.