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

Dynamically imported client component with variable name returns error in dev but works fine in prod #44946

Closed
1 task done
songhobby opened this issue Jan 17, 2023 · 7 comments · Fixed by #50992
Closed
1 task done
Labels
bug Issue was opened via the bug report template.

Comments

@songhobby
Copy link

Verify canary release

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

Provide environment information

Operating System:
Platform: darwin
Arch: x64
Version: Darwin Kernel Version 22.2.0: Fri Nov 11 02:08:47 PST 2022; root:xnu-8792.61.2~4/RELEASE_X86_64
Binaries:
Node: 16.17.0
npm: 8.15.0
Yarn: 1.22.19
pnpm: N/A
Relevant packages:
next: 13.1.2
eslint-config-next: 13.1.2
react: 18.2.0
react-dom: 18.2.0

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

App directory (appDir: true), Dynamic imports (next/dynamic)

Link to the code that reproduces this issue

https://stackblitz.com/edit/vercel-next-js-zxfkvg?file=app/page.tsx

To Reproduce

yarn dev

Describe the Bug

When dynamically importing a client component with variable name which uses React context, next returns error on dev but works fine on prod. Note: dynamic import with static name works in both environment.

Expected Behavior

yarn dev errors and the error says TypeError: Cannot read properties of null (reading 'useContext')
yarn build && yarn start works fine.

Which browser are you using? (if relevant)

Chrome

How are you deploying your application? (if relevant)

Vercel

@songhobby songhobby added the bug Issue was opened via the bug report template. label Jan 17, 2023
@kachkaev
Copy link
Contributor

kachkaev commented Jan 17, 2023

Duplicate of / Related to #44083?

@Fredkiss3
Copy link
Contributor

Sorry if my question is out of context, but you can use next/dynamic for this right ?

@songhobby
Copy link
Author

songhobby commented Jan 17, 2023

Sorry if my question is out of context, but you can use next/dynamic for this right ?

We need a programmatic way to load a component of variable name and it is not officially supported per next note:

Note: In import('path/to/component'), the path must be explicitly written. It can't be a template string nor a variable. Furthermore the import() has to be inside the dynamic() call for Next.js to be able to match webpack bundles / module ids to the specific dynamic() call and preload them before rendering. dynamic() can't be used inside of React rendering as it needs to be marked in the top level of the module for preloading to work, similar to React.lazy.

@songhobby
Copy link
Author

songhobby commented Jan 17, 2023

Duplicate of / Related to #44083?

I don't think hey are caused by the same problem as it has nothing to do with minimized react error
Also the production build works like a breeze.

@huozhi
Copy link
Member

huozhi commented May 26, 2023

confirmed it's the same issue as #49382 , will close this for now and track there

@huozhi huozhi closed this as completed May 26, 2023
@huozhi
Copy link
Member

huozhi commented Jun 10, 2023

This problem is fixed in next@13.4.5-canary.9

@huozhi huozhi linked a pull request Jun 10, 2023 that will close this issue
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants