Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[edge api] allow wasm usage #37836

Merged
merged 2 commits into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions examples/with-webassembly/pages/api/edge.js
@@ -0,0 +1,11 @@
import add_module from '../../add.wasm?module'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a standalone example for WASM usage in the Edge runtime? I think this one is mostly for WASM in Next.js pages.


const instance$ = WebAssembly.instantiate(add_module)

export default async function edgeExample() {
const { exports } = await instance$
const number = exports.add_one(10)
return new Response(`got: ${number}`)
}

export const config = { runtime: 'experimental-edge' }
15 changes: 8 additions & 7 deletions packages/next/build/webpack-config.ts
Expand Up @@ -1716,13 +1716,14 @@ export default async function getBaseWebpackConfig(

const webpack5Config = webpackConfig as webpack5.Configuration

webpack5Config.module?.rules?.unshift({
test: /\.wasm$/,
issuerLayer: 'middleware',
loader: 'next-middleware-wasm-loader',
type: 'javascript/auto',
resourceQuery: /module/i,
})
if (isEdgeServer) {
webpack5Config.module?.rules?.unshift({
test: /\.wasm$/,
loader: 'next-middleware-wasm-loader',
type: 'javascript/auto',
resourceQuery: /module/i,
})
}

webpack5Config.experiments = {
layers: true,
Expand Down
Expand Up @@ -32,6 +32,44 @@ function baseNextConfig(): Parameters<typeof createNext>[0] {
}
}

describe('edge api endpoints can use wasm files', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: {
'pages/api/add.js': `
import { increment } from '../../src/add.js'
export default async (request) => {
const input = Number(request.nextUrl.searchParams.get('input')) || 1;
const value = await increment(input);
return new Response(null, { headers: { data: JSON.stringify({ input, value }) } });
}
export const config = { runtime: 'experimental-edge' };
`,
'src/add.wasm': new FileRef(path.join(__dirname, './add.wasm')),
'src/add.js': `
import wasm from './add.wasm?module'
const instance$ = WebAssembly.instantiate(wasm);

export async function increment(a) {
const { exports } = await instance$;
return exports.add_one(a);
}
`,
},
})
})
afterAll(() => next.destroy())
it('uses the wasm file', async () => {
const response = await fetchViaHTTP(next.url, '/api/add', { input: 10 })
expect(extractJSON(response)).toEqual({
input: 10,
value: 11,
})
})
})

describe('middleware can use wasm files', () => {
let next: NextInstance

Expand Down