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

Fix Vue <Portal> SSR hydration mismatches #2700

Merged
merged 5 commits into from
Aug 23, 2023

Conversation

thecrypticace
Copy link
Contributor

@thecrypticace thecrypticace commented Aug 23, 2023

The <Portal> component in @headlessui/vue uses Vue's native <Teleport> component. However, SSR for Teleport is somewhat complicated and requires cooperation between the library / code using it and the "server" rendering the Vue app for SSR. Tools like Nuxt support SSR-ing <Teleport> but only under certain circumstances. Unfortunately with how we use portals in Headless UI supporting SSR properly would be rather complicated if it's possible at all.

Right now, when used in Nuxt, you can get a hydration error if the thing that's using a Portal is "immediately visible". This means that when Vue goes to hydrate the DOM you'll get a warning in the console like this:

Hydration children mismatch in <div>: server rendered element contains fewer child nodes than client vdom. 
  at <Portal> 
  at …

The Vue docs discuss SSR and <Teleport> in the context of SSR specifically and one of the recommendations is delay mounting of Teleport until the component is mounted.

So, in this PR, we've updated our <Portal> component to defer mounting the portal content until after the component has mounted in the DOM to ensure it matches during SSR hydration.

This always coincides with `onMounted` currently but that’s about to change
This prevent’s SSR hydration issues and matches the behavior of React’s `<Portal>` element
@vercel
Copy link

vercel bot commented Aug 23, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
headlessui-react ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 23, 2023 6:36pm
headlessui-vue ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 23, 2023 6:36pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant