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

NextJS: Aliases react and react-dom like next.js does #23671

Merged
merged 5 commits into from
Sep 19, 2023

Conversation

sookmax
Copy link
Contributor

@sookmax sookmax commented Aug 1, 2023

Closes #23661

Warning: This PR seems to break the rendering of <InlineStory /> in docs pages. Please do not accept this PR until the issue related to the rendering of <InlineStory /> is resolved.

What I did

While I was investigating #23661, I noticed next's webpack resolves react and react-dom differently. Specifically, it resolves them to the pre-compiled versions that reside in next/dist/compiled/react and next/dist/compiled/react-dom respectively: https://github.com/vercel/next.js/blob/ff5338ce03a3240a97a5c84f5ad5c31c0f53a6ce/packages/next/src/build/webpack-config.ts#L1370-L1379.

I speculate these pre-compiled react and react-dom are from Canary channel of React, which contains some undocumented / experimental APIs that are not available in Latest channel.

next uses these experimental versions of react and react-dom because some of their components (e.g., next/image) depends on the APIs provided by them.

To be more specific, the root cause of #23661 is the introduction of a new dependency in next/image (vercel/next.js#52425) that is not available in react-dom@latest: https://github.com/vercel/next.js/blob/7d57784fc0585cb9e5394b982761a840a6554515/packages/next/src/client/image-component.tsx#L13. (i.e., ReactDOM.preload()).

So I added the same aliases to @Storybook/nextjs so that next/image doesn't break in the storybook webpack server.

Unfortunately, this introduces a new bug where the <Canvas /> (or more specifically <InlineStory /> in the Canvas) doesn't render the story component properly:

Screenshot 2023-08-01 at 10 49 42 PM

React does throw an error that says Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node. when I leave the docs page and enter a story page, but other than that I'm pretty much clueless why the change I made affects the rendering of <InlineStory /> 😅.

So, it would be much appreciated if the core team members shed some lights in this issue so that eventually we can resolve #23661.

Thanks!

How to test

Checklist

  • Make sure your changes are tested (stories and/or unit, integration, or end-to-end tests)
  • Make sure to add/update documentation regarding your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli/src/sandbox-templates.ts
  • Make sure this PR contains one of the labels below.

["cleanup", "BREAKING CHANGE", "feature request", "bug", "build", "documentation", "maintenance", "dependencies", "other"]

🦋 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>

@ndelangen ndelangen changed the title Next.js: add webpack aliases to resolve react and react-dom like Next.js does. Next.js: Aliases react and react-dom like next.js does Aug 25, 2023
@yannbf
Copy link
Member

yannbf commented Sep 19, 2023

@tmeasday @valentinpalkovic @ndelangen what are the next steps for this PR?

@ndelangen ndelangen changed the title Next.js: Aliases react and react-dom like next.js does NextJS: Aliases react and react-dom like next.js does Sep 19, 2023
@tmeasday
Copy link
Member

@ndelangen it looks like there is an unresolved problem on this PR?

@ndelangen ndelangen merged commit 2b9bf9a into storybookjs:next Sep 19, 2023
14 of 16 checks passed
@ndelangen ndelangen added the patch:yes Bugfix & documentation PR that need to be picked to main branch label Sep 20, 2023
storybook-bot pushed a commit that referenced this pull request Sep 20, 2023
NextJS: Aliases `react` and `react-dom` like `next.js` does
(cherry picked from commit 2b9bf9a)
@github-actions github-actions bot mentioned this pull request Sep 20, 2023
8 tasks
storybook-bot pushed a commit that referenced this pull request Sep 20, 2023
NextJS: Aliases `react` and `react-dom` like `next.js` does
(cherry picked from commit 2b9bf9a)
@github-actions github-actions bot added the patch:done Patch/release PRs already cherry-picked to main/release branch label Sep 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug ci:normal nextjs patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Using appDirectory in preview.ts causes reactdom.preload error with Next.js framework
5 participants