-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Next.js: Fix bundled react and react-dom in monorepos #29444
Next.js: Fix bundled react and react-dom in monorepos #29444
Conversation
In a pnpm monorepo with multiple Next.js projects, different projects can have different peerDependencies for Next.js, which causes pnpm to install multiple instances of the `next` package in the monorepo. In this situation, prior to this change, Storybook's webpack configuration could consume `react` from one instance of Next and `react-dom` from another, causing React render errors due to the mismatch. This issue was caused by the `configureConfig` function adding webpack module aliases for `react` using `addScopedAlias` (which generates an alias with an absolute filesystem path pointing to the correct instance of `next`, e.g. `'/path/to/next/dist/compiled/react'`), but adding module aliases for `react-dom` using `setAlias` (which generates an alias with a relative path, e.g. `'next/dist/compiled/react-dom'`). This would create a webpack configuration like this: ``` alias: { ⋮ react: '/Users/kyank/Developer/authentication-ui/node_modules/.pnpm/next@14.2.13_@babel+core@7.25.2_@opentelemetry+api@1.8.0_@playwright+test@1.48.1_babel-plugin_z4uy3ayinaafvek4wmyon66ziu/node_modules/next/dist/compiled/react', ⋮ 'react-dom/test-utils': 'next/dist/compiled/react-dom/cjs/react-dom-test-utils.production.js', 'react-dom$': 'next/dist/compiled/react-dom', 'react-dom/client': 'next/dist/compiled/react-dom/client', 'react-dom/server': 'next/dist/compiled/react-dom/server', ⋮ }, ``` This change uses `addScopedAlias` to create aliases with absolute filesystem paths for all of the Next.js-bundled React packages. This fixes the React rendering errors in our monorepo. This issue seems to have been [introduced in Storybook 8.3.0](https://github.com/storybookjs/storybook/pull/29044/files#diff-20144c44999f6f1054f74f56ef1c3fcfcec008fd7b5caea5e10568e95eccb051).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile
@valentinpalkovic Perhaps you could take a look at this? It looks like you introduced the |
☁️ Nx Cloud ReportCI is running/has finished running commands for commit e9ee80d. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this CI Pipeline Execution ✅ Successfully ran 1 targetSent with 💌 from NxCloud. |
The previous implementation supported relative references to packages (e.g. `next/dist/compiled/react-dom`) and named exports (e.g. `next/dist/compiled/react-dom/client`), but not references to physical script files within packages (e.g. `next/dist/compiled/react-dom/cjs/react-dom-test-utils.production.js`). The latter are now handled by detecting when require.resolve returns a path and filename that ends with the exact string provided to the function.
LGTM! CI is green. Let's merge it! |
…norepo Next.js: Fix bundled react and react-dom in monorepos (cherry picked from commit 6c829c5)
What I did
In a pnpm monorepo with multiple Next.js projects, different projects can have different peerDependencies for Next.js, which causes pnpm to install multiple instances of the
next
package in the monorepo. In this situation, Storybook's webpack configuration could consumereact
from one instance of Next andreact-dom
from another, causing React render errors due to the mismatch.This issue was caused by the
configureConfig
function adding webpack module aliases forreact
usingaddScopedAlias
(which generates an alias with an absolute filesystem path pointing to the correct instance ofnext
, e.g.'/path/to/next/dist/compiled/react'
), but adding module aliases forreact-dom
usingsetAlias
(which generates an alias with a relative path, e.g.'next/dist/compiled/react-dom'
).This would create a webpack configuration like this:
This PR updates
configureConfig
to useaddScopedAlias
to create aliases with absolute filesystem paths for all of the Next.js-bundled React packages. This fixes the React rendering errors in our monorepo.This issue seems to have been introduced in Storybook 8.3.0.
Checklist for Contributors
Testing
The changes in this PR are covered in the following automated tests:
Manual testing
It doesn't look like @storybook/nextjs has any tests, so I'm not sure how to test this other than to confirm that this has resolved the issue in our own monorepo of Next.js projects.
Looks like there are some nextjs sandbox templates that we could use to verify manually that this doesn't break anything in Next.js Storybook projects, but I've been unable to figure out how to build and run that sandbox.
Documentation
MIGRATION.MD
Checklist for Maintainers
When this PR is ready for testing, make sure to add
ci:normal
,ci:merged
orci:daily
GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/lib/cli/src/sandbox-templates.ts
Make sure this PR contains one of the labels below:
Available labels
bug
: Internal changes that fixes incorrect behavior.maintenance
: User-facing maintenance tasks.dependencies
: Upgrading (sometimes downgrading) dependencies.build
: Internal-facing build tooling & test updates. Will not show up in release changelog.cleanup
: Minor cleanup style change. Will not show up in release changelog.documentation
: Documentation only changes. Will not show up in release changelog.feature request
: Introducing a new feature.BREAKING CHANGE
: Changes that break compatibility in some way with current major version.other
: Changes that don't fit in the above categories.🦋 Canary release
This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the
@storybookjs/core
team here.core team members can create a canary release here or locally with
gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>
Greptile Summary
This PR modifies the Next.js framework configuration in Storybook to ensure consistent absolute paths for React and ReactDOM dependencies, addressing rendering issues in pnpm monorepo setups.
code/frameworks/nextjs/src/config/webpack.ts
to useaddScopedAlias
for all React-related aliasessetAlias
withaddScopedAlias
for ReactDOM-related aliases to generate absolute filesystem paths