rsnext/errors/middleware-dynamic-wasm-compilation.mdx
Delba de Oliveira 44d1a1cb15
docs: Migrate error messages to MDX and App Router. (#52038)
This PR is part of a larger effort to migrate error messages to MDX and
use App Router: https://github.com/vercel/front/pull/23459
2023-07-05 06:11:16 -07:00

29 lines
903 B
Text

---
title: 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`:
```ts filename="middleware.ts"
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
}
```