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(KeepAlive): adapt keepalive to ssr #3259

Merged
merged 2 commits into from Mar 22, 2021
Merged
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
19 changes: 13 additions & 6 deletions packages/runtime-core/src/components/KeepAlive.ts
Expand Up @@ -77,19 +77,26 @@ const KeepAliveImpl = {
},

setup(props: KeepAliveProps, { slots }: SetupContext) {
const cache: Cache = new Map()
const keys: Keys = new Set()
let current: VNode | null = null

const instance = getCurrentInstance()!
const parentSuspense = instance.suspense

// KeepAlive communicates with the instantiated renderer via the
// ctx where the renderer passes in its internals,
// and the KeepAlive instance exposes activate/deactivate implementations.
// The whole point of this is to avoid importing KeepAlive directly in the
// renderer to facilitate tree-shaking.
const sharedContext = instance.ctx as KeepAliveContext

// 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
}

const cache: Cache = new Map()
const keys: Keys = new Set()
let current: VNode | null = null

const parentSuspense = instance.suspense

const {
renderer: {
p: patch,
Expand Down
12 changes: 12 additions & 0 deletions packages/server-renderer/__tests__/render.spec.ts
Expand Up @@ -8,6 +8,7 @@ import {
defineComponent,
createTextVNode,
createStaticVNode,
KeepAlive,
withCtx
} from 'vue'
import { escapeHtml } from '@vue/shared'
Expand Down Expand Up @@ -604,6 +605,17 @@ function testRender(type: string, render: typeof renderToString) {
})
})

describe('vnode component', () => {
test('KeepAlive', async () => {
const MyComp = {
render: () => h('p', 'hello')
}
expect(await render(h(KeepAlive, () => h(MyComp)))).toBe(
`<!--[--><p>hello</p><!--]-->`
)
})
})

describe('raw vnode types', () => {
test('Text', async () => {
expect(await render(createTextVNode('hello <div>'))).toBe(
Expand Down