Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Importing wasm module no longer works with App Router #53163

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

Closed
1 task done
rossng opened this issue Jul 25, 2023 · 3 comments
Closed
1 task done

Importing wasm module no longer works with App Router #53163

rossng opened this issue Jul 25, 2023 · 3 comments
Labels
bug Issue was opened via the bug report template. stale The issue has not seen recent activity.

Comments

@rossng
Copy link

rossng commented Jul 25, 2023

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.5.0: Thu Jun  8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000
    Binaries:
      Node: 18.10.0
      npm: 9.1.2
      Yarn: 1.22.19
      pnpm: 7.13.4
    Relevant Packages:
      next: 13.4.13-canary.0
      eslint-config-next: N/A
      react: 18.2.0
      react-dom: 18.2.0
      typescript: N/A
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

App Router

Link to the code that reproduces this issue or a replay of the bug

https://github.com/rossng/nextjs-app-router-wasm

To Reproduce

Describe the Bug

You will see the following error printed in your terminal, and the page will fail to load with an Internal Server Error.

Error: 'entryOptions.layer' is only allowed when 'experiments.layers' is enabled
    at EntryOptionPlugin.entryDescriptionToOptions (/Users/ross/Projects/nextjs-wasm/node_modules/next/dist/compiled/webpack/bundle5.js:28:206883)
    at /Users/ross/Projects/nextjs-wasm/node_modules/next/dist/compiled/webpack/bundle5.js:28:205862
- error Error: 'entryOptions.layer' is only allowed when 'experiments.layers' is enabled
    at EntryOptionPlugin.entryDescriptionToOptions (/Users/ross/Projects/nextjs-wasm/node_modules/next/dist/compiled/webpack/bundle5.js:28:206883)
    at /Users/ross/Projects/nextjs-wasm/node_modules/next/dist/compiled/webpack/bundle5.js:28:205862 {
  digest: undefined
}
Error: 'entryOptions.layer' is only allowed when 'experiments.layers' is enabled
    at EntryOptionPlugin.entryDescriptionToOptions (/Users/ross/Projects/nextjs-wasm/node_modules/next/dist/compiled/webpack/bundle5.js:28:206883)
    at /Users/ross/Projects/nextjs-wasm/node_modules/next/dist/compiled/webpack/bundle5.js:28:205862
Error: 'entryOptions.layer' is only allowed when 'experiments.layers' is enabled
    at EntryOptionPlugin.entryDescriptionToOptions (/Users/ross/Projects/nextjs-wasm/node_modules/next/dist/compiled/webpack/bundle5.js:28:206883)
    at /Users/ross/Projects/nextjs-wasm/node_modules/next/dist/compiled/webpack/bundle5.js:28:205862 {
  digest: undefined
}

If you enable the layers experiment in next.config.js, you will see the following error in your browser instead:

Error: Element type is invalid. Received a promise that resolves to: [object Promise]. Lazy element type must resolve to a class or function.

image

Expected Behavior

The page should load and the wasm library should execute correctly.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@rossng rossng added the bug Issue was opened via the bug report template. label Jul 25, 2023
@danidb
Copy link

danidb commented Oct 30, 2023

This is a few months old, but it also tripped me up and I came across this issue...

I don't think this is a bug. WebAssembly instantiation is supposed to be async. In a client component, you would probably want a dynamic import and React.lazy/suspense. I got your example working like this:

next.config.js
Webpack experiments "layers," "asyncWebAssembly" enabled.

app/page-component.jsx
With next/dynamic, i.e. React.lazy and Suspense.

This is especially convenient for loading larger WASM modules.

"use client";

import { useEffect } from "react";
import dynamic from "next/dynamic";

export default dynamic(
  async function PageComponent() {
    const { greet } = await import("../pkg");
    return function PageComponentLoaded() {
      useEffect(() => {
        greet("User");
      }, []);

      return <p>Hello world</p>;
    };
  },
  {
    ssr: false, 
    loading: () => <p>Loading WASM...</p>
  },
);

Note. This will work in dev but next build will probably fail if you omit ssr: false. See #25852.

app/page-component.jsx
A dynamic import in useEffect also works.

export default function PageComponent() {
  useEffect(() => {
    async function _greet() {
      const { greet } = await import("../pkg");
      greet("User");
    }
    _greet();
  }, []);

  return <p>Hello world</p>;
}

@nextjs-bot
Copy link
Collaborator

This issue has been automatically marked as stale due to two years of inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you.

@nextjs-bot nextjs-bot added the stale The issue has not seen recent activity. label Apr 27, 2025
@nextjs-bot
Copy link
Collaborator

This issue has been automatically closed due to two years of inactivity. If you’re still experiencing a similar problem or have additional details to share, please open a new issue following our current issue template. Your updated report helps us investigate and address concerns more efficiently. Thank you for your understanding!

@nextjs-bot nextjs-bot closed this as not planned Won't fix, can't repro, duplicate, stale May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. stale The issue has not seen recent activity.
Projects
None yet
Development

No branches or pull requests

3 participants