Skip to content

[labs/nextjs] reflect: true on props are throwing error #3858

Open

Description

@aneta-kjeldsen

Which package(s) are affected?

SSR (@lit-labs/ssr)

Description

When props are reflected in the component, like count: { type: Number, reflect: true }, I'm getting following error in the dev tools console: extra attributes from the server.

It looks like Next.js is having hard times running hydration because server template and client templet don't match.
There are many cases where we need to reflect props, is there a way to fix this somehow?

Reproduction

The bug can be reproduced here:
https://stackblitz.com/edit/nextjs-lit-ssr-cwhzrk?file=src/simple-greeter.js

  1. Run example
  2. Check DevTools and console

Workaround

Removing reflect: true from property declaration solves the issue

Is this a regression?

No or unsure. This never worked, or I haven't tried before.

Affected versions

Failing: 0.1.1

Browser/OS/Node environment

Browser: Chrome Version 112.0.5615.121

Activity

  1. moved this to 📋 Triaged in Lit Project Boardon Apr 28, 2023
  2. augustjk commented on Apr 28, 2023

    @augustjk
    Member

    I've seen the "extra attributes from the server" errors while trying out solutions for #3418 as well. This might be unavoidable so it's worth considering suppressing this via https://react.dev/reference/react-dom/client/hydrateRoot#suppressing-unavoidable-hydration-mismatch-errors which we'll add to the host element during SSR.

  3. augustjk commented on Apr 28, 2023

    @augustjk
    Member

    Per offline convo with @justinfagnani this is something we could have bespoke handling in the @lit-labs/react wrapper where we're able to check property options and include it to the host element on hydrate/initial render, but that won't exactly help with using bare Lit components like this, so I'm still leaning towards suppressing hydration warning.

  4. self-assigned this
    on Apr 28, 2023
  5. aneta-kjeldsen commented on May 1, 2023

    @aneta-kjeldsen
    Author

    Hi, just a suggestion, not even sure if that can be achieved ...
    Would it be possible to add props that have reflect: true and simple default value set in constructor (strings, numbers, booleans) as part of template render on the server?

  6. augustjk commented on May 1, 2023

    @augustjk
    Member

    The hydration warnings are specifically happening when the server add attributes that weren't included in the original template as props. Having defaults on the server won't prevent that.

    To prevent this hydration warning, we need to either not reflect attributes on the server (I don't think we should do this), or find a way to include the added attribute as props at React hydration time.

    For users of the nextjs plugin or the labs/ssr-react package, I think we might include logic for this on the client jsx functions or patch createElement. Right now, our client versions of jsx and createElement simply re-export the original React ones, but we could patch those like we do for the server environment.

    I do wonder if there's benefit to adding all of this extra logic instead of just adding suppressHydrationWarning={true}.

  7. aneta-kjeldsen commented on May 2, 2023

    @aneta-kjeldsen
    Author

    Thanks for coming back.
    I don't think that suppressHydrationWarning={true} is way forward either. On Next.js page I see that there is a pitfall using this solution: This approach makes hydration slower because your components have to render twice. Be mindful of the user experience on slow connections..
    For now, we are passing all reflected props that have a default value set in constructor, something along those lines:
    in our component class we have:

    export class McButton extends LitElement {
      @property({ type: String, reflect: true })
      public fit: Fit = 'medium';
    }
    

    and then when we use component:
    <mc-button fit="medium">Button<mc-button>
    This seems to solve template mismatch errors, but of course is not ideal. In some cases we also set some extra aria attributes in the firstUpdated life cycle function - this is also causing template mismatches.

  8. justinfagnani commented on May 2, 2023

    @justinfagnani
    Collaborator

    @augustjk the logic to bind reflected properties as attributes could be added both to the React wrapper and to the createElement patch... not sure if it's a good idea, but it's possible.

  9. augustjk commented on May 2, 2023

    @augustjk
    Member

    So the logic would be to patch createElement (and runtime JSX functions) so that if the tag provided is a Lit component, look at the Lit component class, find all properties with reflect: true, if there are no user provided prop for those create an instance to grab default values for them, and provide them to React as props, any user provided prop will override this.

    We really only need to do this on the initial client render/hydration of the React component, but there isn't a flag that we can use for that. React docs suggest creating a separate state to track that https://react.dev/reference/react-dom/client/hydrateRoot#handling-different-client-and-server-content.

  10. augustjk commented on May 3, 2023

    @augustjk
    Member

    We've done a team huddle on this and we're going with adding suppressHydrationWarning client-side for these components.

    @aneta-kjeldsen your concern about the pitfall

    This approach makes hydration slower because your components have to render twice. Be mindful of the user experience on slow connections.

    is mentioned specifically with regards to using a state to do a 2-pass render on the client to explicitly show different content on the client versus what the server rendered.

    React's note about the suppressHydrationWarning flag states

    This only works one level deep, and is intended to be an escape hatch. Don’t overuse it. Unless it’s text content, React still won’t attempt to patch it up, so it may remain inconsistent until future updates.

    In our case the warnings are about mismatching attributes that don't actually change the content of the component from what was rendered on the server versus the client. The nature of reflecting properties to attributes is that the discrepancy is expected and therefore okay to suppress.

    As we're talking about attributes and not text content, React "won't attempt to patch it" so there's actually no extra work happening during hydration due to this mismatch.

    Lastly, we're considering the best fix for #3418 to be adding defer-hydration attribute to the host element so that we can imperatively add initial properties to the element before triggering the element's initial update on the client. The addition of the defer-hydration can trigger the same warning during React's hydration which we still consider to be innocuous and should suppress.

    An interesting observation I had is even without using the @lit-labs/nextjs plugin, using Lit components with attribute reflecting properties emits the same warning because the element already contains the reflected attribute from the custom element upgrading by the time React tries to hydrate, even though the HTML from the server never contained the attribute.

  11. aneta-kjeldsen commented on May 3, 2023

    @aneta-kjeldsen
    Author

    Thanks for looking into it.

  12. augustjk commented on May 11, 2023

    @augustjk
    Member

    Discussed this case a bit more during our eng meeting just now. Since the suppressHydrationWarning prop needs to be set on the client side, we can easily do this for components using @lit-labs/react which we're planning to add with #3885.

    But for bare custom elements, we'd need to also patch client-side createElement or jsx-runtime to add the prop, which currently are just passthroughs of the React ones.

    Or we need to do extra logic during SSR to not reflect attributes if they come from default prop which might be hard to do. (This could become much simpler to do with new decorators but that'll still take some time)

    For the time being we're looking to not do anything for this case for bare custom elements. The hydration warnings only happen during dev mode and could be ignored. Or, users would have to add the default value in as JSX prop, or add the suppressHydrationWarning themselves to the custom element to prevent the warning.

    We'll definitely revisit this when the better custom element support for React lands (maybe React 19?) as we'll likely need to touch client side element creation then.

    @aneta-kjeldsen Sorry this ultimately means we're not solving your specific use case. Is getting rid of the hydration warning for bare custom elements a high priority for you?

  13. aneta-kjeldsen commented on May 12, 2023

    @aneta-kjeldsen
    Author

    For now, as an interim solution, we can just ask users to pass all reflected props with default values to our components - that's fine. For other attributes (like aria-labels) we will have to find another solution - maybe move the logic from firstUpdated to some other life cycle method.

  14. augustjk commented on Oct 16, 2023

    @augustjk
    Member

    I think it would be good to add an option to the plugin that will add suppressHydrationWarning prop to Lit components client side.

    The option could even take an array of tag names to add the prop only to those tag names, or just a true for suppress for all registered custom elements, maybe..

  15. changed the title [-][@lit-labs/nextjs] `reflect: true` on props are throwing error[/-] [+][labs/nextjs] `reflect: true` on props are throwing error[/+] on Oct 16, 2023
  16. augustjk commented on Oct 27, 2023

    @augustjk
    Member

    Discussed some more options with @michaelwarren1106 about this.

    Plugin option to suppress hydration for custom elements, and making it take a list of custom elements or a pattern match (like prefix) on the tag name, would be a potential stop gap for this.

    The "proper" solution might involve reading the registered component class, and if it's a Lit element, try to read property option to see if it reflects, and try and get the default value and include that extra as a prop, but only do this for the very first time.

  17. augustjk commented on Jan 5, 2024

    @augustjk
    Member

    useSyncExternalStore apparently has a code path that'll run on the server and on hydration, which allows us to have some bespoke logic there. Source https://twitter.com/tkdodo/status/1741068994981826947 and credit to @ceIia for suggesting we might make use of this.

    Unfortunately, while we can use this to detect the hydration pass, that still means creating a React component wrapper for the custom element, do the inspection for the reflecting attributes, and provide those to React as props to satisfy the hydration check. For bare custom elements in JSX, that means monkeypatching React.createElement or an alternate jsx-runtime for the client too.

  18. sacitgonen commented on Feb 12, 2025

    @sacitgonen

    Is there any update on this issue? I wanted to bring attention to it. We are developing a web component library and encountering a hydration error with Next.js 15 (React 19). As mentioned in the issue, we are getting a hydration error when default-valued properties try to reflect back into the DOM. Should we consider abandoning reflect: true in our components?

  19. justinfagnani commented on Feb 12, 2025

    @justinfagnani
    Collaborator

    @sorvell is working on #4895 which will help in some of these cases - when the reflection is happening because of the initial value. Are the other cases - when reflection is happening because of the associated property being set - a problem too? Is it only a problem if the property and attribute name are different?

    A good step forward here would be to add some failing test cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions