Skip to content

Prevent errors from comment node roots with enableViewTransition #33205

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

Merged
merged 2 commits into from
May 21, 2025

Conversation

jackpope
Copy link
Member

We have many cases internally where the containerInstance resolves to a comment node. restoreRootViewTransitionName is called when enableViewTransition is on, even without introducing a <ViewTransition />. So that means it can crash pages because containerInstance.style is undefined just by turning on the flag.

This adds a check for undefined so we can roll the flag out more broadly.

Copy link
Collaborator

@sebmarkbage sebmarkbage left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any logic around comment nodes should be gated on disableCommentsAsDOMContainers.

However, I don't think it's quite the right fix just to cover it up though.

It also needs to pair with cancelRootViewTransitionName. If we just cover it up, we might instead cause bugs that leave things not cleaned up.

I think maybe the issue here is that we're calling restoreRootViewTransitionName even if no transition was started. We should probably ideally gate that call on if we called the cancelRootViewTransitionName. That way if you didn't start a ViewTransition this error can't happen.

However, if a ViewTransition started in one of these roots, what should happen?

Comment on lines 1596 to 1618
if (
containerInstance.style &&
containerInstance.style.viewTransitionName === 'root'
) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (
containerInstance.style &&
containerInstance.style.viewTransitionName === 'root'
) {
if (
(disableCommentsAsDOMContainers || containerInstance.nodeType !== COMMENT_NODE) &&
// $FlowFixMe[prop-missing]
containerInstance.style.viewTransitionName === 'root'
) {

Since most of this applying and restoring only applies to the documentElement which we get even if it's a comment. This code is mainly gated on the gesture case. So we can ignore it if it's not a gesture case.

However, I think we probably should throw in the gesture case if it's a comment container so that it throws before getting to the restore.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jackpope
Copy link
Member Author

However, if a ViewTransition started in one of these roots, what should happen?

An alternative could be to take the same path as rootContainer.nodeName === 'HTML' and use the ownerDocument.body from the comment node. Though maybe that results in multiple comment roots clashing over the same page document naming.

Or we could skip the clear/restore of the root naming altogether for comment nodes and provide a dev warning that you are have a ViewTransition with a comment root and it will be a global animation by default.

@jackpope jackpope force-pushed the view-transition-comment-containers branch from 987e85e to 9536fc1 Compare May 20, 2025 20:45
@jackpope jackpope force-pushed the view-transition-comment-containers branch from 9536fc1 to 33b5931 Compare May 20, 2025 20:47
@react-sizebot
Copy link

Comparing: 50389e1...33b5931

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 6.68 kB 6.68 kB = 1.83 kB 1.83 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 529.83 kB 529.83 kB = 93.52 kB 93.52 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 6.69 kB 6.69 kB +0.05% 1.83 kB 1.83 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js +0.02% 651.57 kB 651.73 kB +0.03% 114.78 kB 114.81 kB
facebook-www/ReactDOM-prod.classic.js +0.03% 675.81 kB 676.00 kB +0.04% 118.87 kB 118.91 kB
facebook-www/ReactDOM-prod.modern.js +0.03% 666.09 kB 666.28 kB +0.04% 117.26 kB 117.30 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by 🚫 dangerJS against 33b5931

@jackpope
Copy link
Member Author

Updated to skip the cancel/restore and warn in dev

@jackpope jackpope requested a review from sebmarkbage May 20, 2025 21:16
@jackpope jackpope merged commit 3710c4d into facebook:main May 21, 2025
240 checks passed
@jackpope jackpope deleted the view-transition-comment-containers branch May 21, 2025 17:57
github-actions bot pushed a commit that referenced this pull request May 21, 2025
)

We have many cases internally where the `containerInstance` resolves to
a comment node. `restoreRootViewTransitionName` is called when
`enableViewTransition` is on, even without introducing a
`<ViewTransition />`. So that means it can crash pages because
`containerInstance.style` is `undefined` just by turning on the flag.

This skips cancel/restore of root view transition name if a comment node is the root.

DiffTrain build for [3710c4d](3710c4d)
github-actions bot pushed a commit to code/lib-react that referenced this pull request May 21, 2025
…ebook#33205)

We have many cases internally where the `containerInstance` resolves to
a comment node. `restoreRootViewTransitionName` is called when
`enableViewTransition` is on, even without introducing a
`<ViewTransition />`. So that means it can crash pages because
`containerInstance.style` is `undefined` just by turning on the flag.

This skips cancel/restore of root view transition name if a comment node is the root.

DiffTrain build for [3710c4d](facebook@3710c4d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants