Next.js: Fix bundled react and react-dom in monorepos#29444
Merged
valentinpalkovic merged 4 commits intoNov 1, 2024
Merged
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).
Contributor
There was a problem hiding this comment.
1 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile
Contributor
Author
|
@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. |
valentinpalkovic
requested changes
Oct 30, 2024
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.
valentinpalkovic
approved these changes
Nov 1, 2024
Contributor
|
LGTM! CI is green. Let's merge it! |
11 tasks
Merged
7 tasks
shilman
pushed a commit
that referenced
this pull request
Nov 5, 2024
…norepo Next.js: Fix bundled react and react-dom in monorepos (cherry picked from commit 6c829c5)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
nextpackage in the monorepo. In this situation, Storybook's webpack configuration could consumereactfrom one instance of Next andreact-domfrom another, causing React render errors due to the mismatch.This issue was caused by the
configureConfigfunction adding webpack module aliases forreactusingaddScopedAlias(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-domusingsetAlias(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
configureConfigto useaddScopedAliasto 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:mergedorci:dailyGH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found incode/lib/cli/src/sandbox-templates.tsMake 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/coreteam 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.tsto useaddScopedAliasfor all React-related aliasessetAliaswithaddScopedAliasfor ReactDOM-related aliases to generate absolute filesystem paths