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

hydrating vue custom element with key prop will cause unexpected re-mount and lose parent context #11641

Closed
lejunyang opened this issue Aug 17, 2024 · 1 comment · Fixed by #11655
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. has workaround A workaround has been found to avoid the problem scope: custom elements scope: ssr

Comments

@lejunyang
Copy link

lejunyang commented Aug 17, 2024

Vue version

3.4.38

Link to minimal reproduction

https://stackblitz.com/edit/vite-778jll?file=docs%2F.vitepress%2Ftheme%2FLayout.vue

Steps to reproduce

open the console, wait for the build and preview, observe the logs

What is expected?

child element should not be unmounted, inject in child element should work as expected

What is actually happening?

we can see from console's logs, all child elements are unmounted and mounted again, then inject is not working.
image

If we remove key prop from my-test element in Layout.vue, and run npm run test again, we can see they all work as expected.

Hydration performs patchProp for custom element, then we get new props with key and that triggers custom element updating. But old vnode doesn't have a key, which leads to false from isSameVNodeType, unmount happens

    // patching & not same type, unmount old tree
    if (n1 && !isSameVNodeType(n1, n2)) {
      anchor = getNextHostNode(n1)
      unmount(n1, parentComponent, parentSuspense, true)
      n1 = null
    }

Parent component is lost during new mount so inject doesn't work

System Info

No response

Any additional comments?

I can see hydration will finally add key as an attribute for custom element. Adding that in advance when renderToString can avoid unmount. Not sure if it's good solution

@edison1105 edison1105 added scope: ssr scope: custom elements has workaround A workaround has been found to avoid the problem 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. and removed scope: ssr labels Aug 19, 2024
@edison1105
Copy link
Member

minimal reproduction

@github-actions github-actions bot locked and limited conversation to collaborators Sep 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. has workaround A workaround has been found to avoid the problem scope: custom elements scope: ssr
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants