mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
bc59f210b0
This PR removes middleware docs and adds a "Migration to Proxy" section to the Proxy docs, which explains the rationale for the renaming to Proxy and provides a migration guide. Did not remove `middleware-upgrade-docs` as it's an upgrade doc from the legacy middleware. Below are untouched as they need codebase changes: - `instrumentation` docs - `onRequestError` has `context.routeType` as `'middleware'` - `adapterPath` docs - has `MiddlewareMatcher` type example - `ProxyConfig` - has a user-facing `MiddlewareMatcher` type --------- Co-authored-by: Joseph Chamochumbi <joseph.chamochumbi@vercel.com>
48 lines
1.7 KiB
Plaintext
48 lines
1.7 KiB
Plaintext
---
|
|
title: Dynamic code evaluation is not available in Proxy
|
|
---
|
|
|
|
## Why This Error Occurred
|
|
|
|
`eval()`, `new Function()` or compiling WASM binaries dynamically is not allowed in Proxy.
|
|
Specifically, the following APIs are not supported:
|
|
|
|
- `eval()`
|
|
- `new Function()`
|
|
- `WebAssembly.compile`
|
|
- `WebAssembly.instantiate` with [a buffer parameter](https://developer.mozilla.org/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`:
|
|
|
|
```ts filename="proxy.ts"
|
|
import { NextResponse } from 'next/server'
|
|
import squareWasm from './square.wasm?module'
|
|
|
|
export default async function proxy() {
|
|
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 tree-shaking.
|
|
You can relax the check to allow specific files with your Proxy [configuration](/docs/pages/api-reference/edge#unsupported-apis):
|
|
|
|
```ts filename="pages/api/example.ts"
|
|
export const config = {
|
|
unstable_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
|
|
],
|
|
}
|
|
```
|
|
|
|
`unstable_allowDynamic` is a glob, 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 cause a runtime error_.
|