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(Suspense): switch suspense cause error in the Keepalive (fix: #6416) #6467

Closed
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion packages/runtime-core/__tests__/components/Suspense.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
watchEffect,
onUnmounted,
onErrorCaptured,
shallowRef
shallowRef,
KeepAlive
} from '@vue/runtime-test'
import { createApp } from 'vue'

Expand Down Expand Up @@ -1253,4 +1254,54 @@ describe('Suspense', () => {
`A component with async setup() must be nested in a <Suspense>`
).toHaveBeenWarned()
})

// #6416

test('test keepalive with suspense', async () => {
const Async = defineAsyncComponent({
render() {
return h('div', 'async')
}
})
const sync = {
render() {
return h('div', 'sync')
}
}
const components = [Async, sync]
const viewRef = ref(0)
const root = nodeOps.createElement('div')
const App = {
render() {
return h(KeepAlive, null, {
default: () => {
return h(Suspense, null, {
default: h(components[viewRef.value]),
fallback: h('div', 'Loading-dynamic-components')
})
}
})
}
}
render(h(App), root)
expect(serializeInner(root)).toBe(`<div>Loading-dynamic-components</div>`)

viewRef.value = 1
await nextTick()
expect(serializeInner(root)).toBe(`<div>sync</div>`)

viewRef.value = 0
await nextTick()

expect(serializeInner(root)).toBe('<!---->')

await Promise.all(deps)
await nextTick()
// when async resolve,it should be <div>async</div>
expect(serializeInner(root)).toBe('<div>async</div>')

viewRef.value = 1
await nextTick() //TypeError: Cannot read properties of null (reading 'parentNode'),This has been fixed
expect(serializeInner(root)).toBe(`<div>sync</div>`)
})
})
5 changes: 3 additions & 2 deletions packages/runtime-core/src/components/Suspense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ function patchSuspense(
triggerEvent(n2, 'onPending')
// mount pending branch in off-dom container
suspense.pendingBranch = newBranch
suspense.pendingId++
if (suspense.pendingId > 0) suspense.pendingId--
Copy link
Member

@edison1105 edison1105 Aug 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the root cause is: the newBranch is cached in keepalive and its suspenseId not updated when pendingId++. if we update the suspenseId of newBranch will be ok.

likely:

if (
  parentComponent &&
  (parentComponent.vnode.type as any).__isKeepAlive &&
  newBranch.component
) {
  newBranch.component.suspenseId = suspense.pendingId
}

patch(
null,
newBranch,
Expand Down Expand Up @@ -775,7 +775,8 @@ export function queueEffectWithSuspense(
function setActiveBranch(suspense: SuspenseBoundary, branch: VNode) {
suspense.activeBranch = branch
const { vnode, parentComponent } = suspense
const el = (vnode.el = branch.el)
const el = branch.el
if (el) vnode.el = el
// in case suspense is the root node of a component,
// recursively update the HOC el
if (parentComponent && parentComponent.subTree === vnode) {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-dom/src/nodeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const nodeOps: Omit<RendererOptions<Node, Element>, 'patchProp'> = {
el.textContent = text
},

parentNode: node => node.parentNode as Element | null,
parentNode: node => (node ? node.parentNode : null) as Element | null,
Copy link
Member

@edison1105 edison1105 Aug 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is unnecessary.


nextSibling: node => node.nextSibling,

Expand Down
2 changes: 1 addition & 1 deletion packages/runtime-test/src/nodeOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ function setElementText(el: TestElement, text: string) {
}

function parentNode(node: TestNode): TestElement | null {
return node.parentNode
return node ? node.parentNode : null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is unnecessary.

}

function nextSibling(node: TestNode): TestNode | null {
Expand Down