fix(ssr): properly update currentRenderingInstance state during ssr

fix #2863
This commit is contained in:
Evan You
2021-03-26 14:00:03 -04:00
parent 9036f88d83
commit 8c3c14a0ff
4 changed files with 51 additions and 8 deletions

View File

@@ -9,11 +9,23 @@ import { closeBlock, openBlock } from './vnode'
export let currentRenderingInstance: ComponentInternalInstance | null = null
export let currentScopeId: string | null = null
/**
* Note: rendering calls maybe nested. The function returns the parent rendering
* instance if present, which should be restored after the render is done:
*
* ```js
* const prev = setCurrentRenderingInstance(i)
* // ...render
* setCurrentRenderingInstance(prev)
* ```
*/
export function setCurrentRenderingInstance(
instance: ComponentInternalInstance | null
) {
): ComponentInternalInstance | null {
const prev = currentRenderingInstance
currentRenderingInstance = instance
currentScopeId = (instance && instance.type.__scopeId) || null
return prev
}
/**
@@ -40,8 +52,7 @@ export function withCtx(
if (!isRenderingCompiledSlot) {
openBlock(true /* null block that disables tracking */)
}
const prevInstance = currentRenderingInstance
setCurrentRenderingInstance(ctx)
const prevInstance = setCurrentRenderingInstance(ctx)
const res = fn(...args)
setCurrentRenderingInstance(prevInstance)
if (!isRenderingCompiledSlot) {

View File

@@ -53,7 +53,7 @@ export function renderComponentRoot(
} = instance
let result
setCurrentRenderingInstance(instance)
const prev = setCurrentRenderingInstance(instance)
if (__DEV__) {
accessedAttrs = false
}
@@ -207,7 +207,7 @@ export function renderComponentRoot(
result = createVNode(Comment)
}
setCurrentRenderingInstance(null)
setCurrentRenderingInstance(prev)
return result
}