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(runtime-core): fix keep-alive component causing hydration node mismatch (fix #4817) #5636

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/runtime-core/src/components/KeepAlive.ts
Expand Up @@ -96,7 +96,19 @@ const KeepAliveImpl: ComponentOptions = {
// if the internal renderer is not registered, it indicates that this is server-side rendering,
// for KeepAlive, we just need to render its children
if (!sharedContext.renderer) {
return slots.default
return () => {
if (!slots.default) {
return null
}

const children = slots.default()
if (children.length > 1) {
warn(`KeepAlive should contain exactly one component child.`)
return children
}

return children[0]
}
}

const cache: Cache = new Map()
Expand Down
2 changes: 1 addition & 1 deletion packages/server-renderer/__tests__/render.spec.ts
Expand Up @@ -676,7 +676,7 @@ function testRender(type: string, render: typeof renderToString) {
render: () => h('p', 'hello')
}
expect(await render(h(KeepAlive, () => h(MyComp)))).toBe(
`<!--[--><p>hello</p><!--]-->`
`<p>hello</p>`
)
})

Expand Down