Skip to content

Commit e8e9b00

Browse files
authored
fix(KeepAlive): adapt keepalive for ssr (#3259)
fix #3255
1 parent 602b58e commit e8e9b00

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

packages/runtime-core/src/components/KeepAlive.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,26 @@ const KeepAliveImpl = {
7777
},
7878

7979
setup(props: KeepAliveProps, { slots }: SetupContext) {
80-
const cache: Cache = new Map()
81-
const keys: Keys = new Set()
82-
let current: VNode | null = null
83-
8480
const instance = getCurrentInstance()!
85-
const parentSuspense = instance.suspense
86-
8781
// KeepAlive communicates with the instantiated renderer via the
8882
// ctx where the renderer passes in its internals,
8983
// and the KeepAlive instance exposes activate/deactivate implementations.
9084
// The whole point of this is to avoid importing KeepAlive directly in the
9185
// renderer to facilitate tree-shaking.
9286
const sharedContext = instance.ctx as KeepAliveContext
87+
88+
// if the internal renderer is not registered, it indicates that this is server-side rendering,
89+
// for KeepAlive, we just need to render its children
90+
if (!sharedContext.renderer) {
91+
return slots.default
92+
}
93+
94+
const cache: Cache = new Map()
95+
const keys: Keys = new Set()
96+
let current: VNode | null = null
97+
98+
const parentSuspense = instance.suspense
99+
93100
const {
94101
renderer: {
95102
p: patch,

packages/server-renderer/__tests__/render.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
defineComponent,
99
createTextVNode,
1010
createStaticVNode,
11+
KeepAlive,
1112
withCtx
1213
} from 'vue'
1314
import { escapeHtml } from '@vue/shared'
@@ -604,6 +605,17 @@ function testRender(type: string, render: typeof renderToString) {
604605
})
605606
})
606607

608+
describe('vnode component', () => {
609+
test('KeepAlive', async () => {
610+
const MyComp = {
611+
render: () => h('p', 'hello')
612+
}
613+
expect(await render(h(KeepAlive, () => h(MyComp)))).toBe(
614+
`<!--[--><p>hello</p><!--]-->`
615+
)
616+
})
617+
})
618+
607619
describe('raw vnode types', () => {
608620
test('Text', async () => {
609621
expect(await render(createTextVNode('hello <div>'))).toBe(

0 commit comments

Comments
 (0)