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
Web Component Complex Values not working as expected on latest canary #65584
Comments
This has the desired outcome, right? The current implementation of custom elements in react is only at step 1 as I wrote in my doc, so it is best to work around server rendering and hydration of custom elements as you did in the second web component because react can't know how to apply props to a not-yet-defined custom element. This is the first feedback about server rendering and hydration I've gotten since developing the improved custom elements in react feature years ago, so this is a good signal that we should consider implementing step 2 in my doc to make it easier to do the workaround that you made in the second web component. (I may be mis-interpreting what's going on here, I don't understand next.js very well and I didn't dive in very deep to the codesandbox) |
All good! Here's a more detailed breakdown of the situation. With respect to different client+server permutations on Next.js, we've got 3 relevant cases:
Example: const MyReactComponent = dynamic(() => import('./MyReactComponent'), {
loading: () => <p>Loading...</p>,
ssr: false, // This is what ensures the component will be instantiated *entirely* on the client side
});
// ...
const MyContainerComponent = () => {
// ...
return <MyReactComponent/>; // Using the component, including on an RSC
};
Ideally, imo, we want all 3 permutations to work smoothly with web components for relevant use cases. (1) should only be necessary if a web component author does not e.g. provide appropriate shims for Web APIs defined on (2) is the most likely desired permutation for most web component + Next.js use cases. The problem with the current behavior is that something will end up being set as an attribute on the partial hydration server side, but never get set as a property on the second pass on the client side, including the cases I described in facebook/react#29037. This effectively makes partial hydration unpredictable and likely to be avoided for (almost?) all React + web component usage. I also think this is a reason to ensure things like objects are never set as attributes (per my discussion in the linked react issue). (3) is also useful, but likely only for a subset of web components (e.g. custom container web components, display-only web components, and the like, all only if they can be useful with only attributes). |
Thanks! I think I have a more comprehensive response to this issue at large in the React issue: facebook/react#29037 (comment) I think that your example code in case 1 if fleshed out could be very valuable documentation for other people using the new custom elements in React support in next.js |
Link to the code that reproduces this issue
https://codesandbox.io/p/devbox/jovial-panka-9slgt3
To Reproduce
'use client'
) never gets hydrated with the complex value.'use client'
) never gets hydrated with the nativeonclick
event handler callback, but does get the React built-inonClick
(synthetic) event handler callback.dynamic
loading andssr: false
) does get all complex values set via the web component's properties and new automatic event listener adding/removal.Current vs. Expected behavior
NOTE: This is on canary for react 19 beta, so these may already be on the the team's radar, in which case feel free to close! Also feel free to reach out if any assistance with nextjs + react 19 + web components would be helpful!
Per standard hydration behavior and what I believe were the intentions of @josepharhar's work on the design here, I'd expect complex values to be populated on the client after initial load, e.g.:
Provide environment information
Which area(s) are affected? (Select all that apply)
Not sure
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local), next start (local), Vercel (Deployed)
Additional context
Per callout above, I'm happy to help out with any additional questions/comments/concerns, either for this particular issue or with getting Next + React 19 + Web Components into a good spot.
The text was updated successfully, but these errors were encountered: