Navigation Menu

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

fix: usage of wasm in an appDir page file using the edge runtime #41689

Merged
merged 5 commits into from Nov 3, 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
9 changes: 8 additions & 1 deletion packages/next/build/utils.ts
Expand Up @@ -52,6 +52,7 @@ import {
loadRequireHook,
overrideBuiltInReactPackages,
} from './webpack/require-hook'
import { AssetBinding } from './webpack/loaders/get-module-build-info'

loadRequireHook()
if (process.env.NEXT_PREBUNDLED_REACT) {
Expand Down Expand Up @@ -1259,7 +1260,13 @@ export async function isPageStatic({
const runtime = await getRuntimeContext({
paths: edgeInfo.files.map((file: string) => path.join(distDir, file)),
env: edgeInfo.env,
edgeFunctionEntry: edgeInfo,
edgeFunctionEntry: {
...edgeInfo,
wasm: (edgeInfo.wasm ?? []).map((binding: AssetBinding) => ({
...binding,
filePath: path.join(distDir, binding.filePath),
})),
},
Comment on lines +1263 to +1269
Copy link
Contributor

Choose a reason for hiding this comment

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

good catch :clapping-inclusive:

Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we need to do the same for blob assets? (i.e. fetch(new URL("./file.png", import.meta.url)))

anyway that can be in a new PR :open_source_parrot:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question 🤔 yeah I can take a look and add a test case as a follow-up!

name: edgeInfo.name,
useCache: true,
distDir,
Expand Down
Binary file not shown.
63 changes: 63 additions & 0 deletions test/production/app-dir-edge-runtime-with-wasm/index.test.ts
@@ -0,0 +1,63 @@
import path from 'path'
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'

const files = {
'app/layout.jsx': `
export default function AppLayout({ children }) {
return (
<html>
<head>
<title>WASM Import</title>
</head>
<body>
{children}
</body>
</html>
)
}
`,
'app/page.jsx': `
import wasm from '../wasm/add.wasm?module'
const instance$ = WebAssembly.instantiate(wasm);

async function addOne(a) {
const { exports } = await instance$;
return exports.add_one(a);
}

export default async function Page() {
const two = await addOne(1)
return \`1 + 1 is: $\{two}\`
}

export const runtime = "experimental-edge"
`,
'wasm/add.wasm': new FileRef(path.join(__dirname, 'add.wasm')),
}

describe('app-dir edge runtime with wasm', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure if it would be preferred to have all of the app-dir related tests in test/e2e/app-dir, let me know!

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's good. We can always merge tests. Extracting is harder. 🙏

let next: NextInstance

beforeAll(async () => {
next = await createNext({
files,
dependencies: {
react: 'experimental',
'react-dom': 'experimental',
},
nextConfig: {
experimental: {
appDir: true,
},
},
})
})
afterAll(() => next.destroy())

it('should have built', async () => {
const html = await renderViaHTTP(next.url, '/')
expect(html).toContain('1 + 1 is: 2')
})
})