Skip to content

Commit

Permalink
chore: iterates on PR comments and improves error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
feugy committed Sep 12, 2022
1 parent 75fc85d commit d26017b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/api-reference/edge-runtime.md
Expand Up @@ -139,7 +139,7 @@ The following JavaScript language features are disabled, and **will not work:**
- `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.
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 treeshaking.
You can relax the check to allow specific files with your Middleware or Edge API Route exported configuration:

```javascript
Expand All @@ -154,7 +154,7 @@ export const config = {

`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_.
Be warned that if these statements are executed on the Edge, _they will throw and cause a runtime error_.

## Related

Expand Down
4 changes: 2 additions & 2 deletions errors/edge-dynamic-code-evaluation.md
Expand Up @@ -28,7 +28,7 @@ export default async function middleware() {
}
```

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.
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 treeshaking.
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_.
Be warned that if these statements are executed on the Edge, _they will throw and cause a runtime error_.
25 changes: 25 additions & 0 deletions errors/middleware-dynamic-wasm-compilation.md
@@ -0,0 +1,25 @@
# 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
}
```
9 changes: 7 additions & 2 deletions packages/next/build/analysis/get-page-static-info.ts
Expand Up @@ -164,6 +164,7 @@ function getMiddlewareMatchers(
}

function getMiddlewareConfig(
pageFilePath: string,
config: any,
nextConfig: NextConfig
): Partial<MiddlewareConfig> {
Expand All @@ -182,7 +183,7 @@ function getMiddlewareConfig(
matcher(glob)
} catch (err) {
throw new Error(
`A middleware/edge exported 'config.allowDynamic' is not a valid pattern: ${
`${pageFilePath} exported 'config.allowDynamic' contains invalid pattern '${glob}': ${
(err as Error).message
}`
)
Expand Down Expand Up @@ -291,7 +292,11 @@ export async function getPageStaticInfo(params: {
warnAboutExperimentalEdgeApiFunctions()
}

const middlewareConfig = getMiddlewareConfig(config, nextConfig)
const middlewareConfig = getMiddlewareConfig(
page ?? 'middleware/edge API route',
config,
nextConfig
)

return {
ssr,
Expand Down
2 changes: 1 addition & 1 deletion test/production/edge-config-validations/index.test.ts
Expand Up @@ -29,7 +29,7 @@ describe('Edge config validations', () => {
})
await expect(next.start()).rejects.toThrow('next build failed')
expect(next.cliOutput).toMatch(
`exported 'config.allowDynamic' is not a valid pattern: Expected pattern to be a non-empty string`
`/middleware exported 'config.allowDynamic' contains invalid pattern 'true': Expected pattern to be a non-empty string`
)
})
})

0 comments on commit d26017b

Please sign in to comment.