fix(useId): ensure useId consistency when using serverPrefetch (#12128)
close #12102
This commit is contained in:
parent
ec917cfdb9
commit
b4d35349d8
|
@ -8,6 +8,7 @@ import {
|
||||||
defineAsyncComponent,
|
defineAsyncComponent,
|
||||||
defineComponent,
|
defineComponent,
|
||||||
h,
|
h,
|
||||||
|
onServerPrefetch,
|
||||||
useId,
|
useId,
|
||||||
} from 'vue'
|
} from 'vue'
|
||||||
import { renderToString } from '@vue/server-renderer'
|
import { renderToString } from '@vue/server-renderer'
|
||||||
|
@ -145,6 +146,40 @@ describe('useId', () => {
|
||||||
expect(await getOutput(() => factory(16, 0))).toBe(expected)
|
expect(await getOutput(() => factory(16, 0))).toBe(expected)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('components with serverPrefetch', async () => {
|
||||||
|
const factory = (): ReturnType<TestCaseFactory> => {
|
||||||
|
const SPOne = defineComponent({
|
||||||
|
setup() {
|
||||||
|
onServerPrefetch(() => {})
|
||||||
|
return () => h(BasicComponentWithUseId)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const SPTwo = defineComponent({
|
||||||
|
render() {
|
||||||
|
return h(BasicComponentWithUseId)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const app = createApp({
|
||||||
|
setup() {
|
||||||
|
const id1 = useId()
|
||||||
|
const id2 = useId()
|
||||||
|
return () => [id1, ' ', id2, ' ', h(SPOne), ' ', h(SPTwo)]
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return [app, []]
|
||||||
|
}
|
||||||
|
|
||||||
|
const expected =
|
||||||
|
'v-0 v-1 ' + // root
|
||||||
|
'v-0-0 v-0-1 ' + // inside first async subtree
|
||||||
|
'v-2 v-3' // inside second async subtree
|
||||||
|
// assert different async resolution order does not affect id stable-ness
|
||||||
|
expect(await getOutput(() => factory())).toBe(expected)
|
||||||
|
expect(await getOutput(() => factory())).toBe(expected)
|
||||||
|
})
|
||||||
|
|
||||||
test('async setup()', async () => {
|
test('async setup()', async () => {
|
||||||
const factory = (
|
const factory = (
|
||||||
delay1: number,
|
delay1: number,
|
||||||
|
|
|
@ -856,11 +856,10 @@ function setupStatefulComponent(
|
||||||
// 2. call setup()
|
// 2. call setup()
|
||||||
const { setup } = Component
|
const { setup } = Component
|
||||||
if (setup) {
|
if (setup) {
|
||||||
|
pauseTracking()
|
||||||
const setupContext = (instance.setupContext =
|
const setupContext = (instance.setupContext =
|
||||||
setup.length > 1 ? createSetupContext(instance) : null)
|
setup.length > 1 ? createSetupContext(instance) : null)
|
||||||
|
|
||||||
const reset = setCurrentInstance(instance)
|
const reset = setCurrentInstance(instance)
|
||||||
pauseTracking()
|
|
||||||
const setupResult = callWithErrorHandling(
|
const setupResult = callWithErrorHandling(
|
||||||
setup,
|
setup,
|
||||||
instance,
|
instance,
|
||||||
|
@ -870,12 +869,16 @@ function setupStatefulComponent(
|
||||||
setupContext,
|
setupContext,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
const isAsyncSetup = isPromise(setupResult)
|
||||||
resetTracking()
|
resetTracking()
|
||||||
reset()
|
reset()
|
||||||
|
|
||||||
if (isPromise(setupResult)) {
|
if ((isAsyncSetup || instance.sp) && !isAsyncWrapper(instance)) {
|
||||||
// async setup, mark as async boundary for useId()
|
// async setup / serverPrefetch, mark as async boundary for useId()
|
||||||
if (!isAsyncWrapper(instance)) markAsyncBoundary(instance)
|
markAsyncBoundary(instance)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isAsyncSetup) {
|
||||||
setupResult.then(unsetCurrentInstance, unsetCurrentInstance)
|
setupResult.then(unsetCurrentInstance, unsetCurrentInstance)
|
||||||
if (isSSR) {
|
if (isSSR) {
|
||||||
// return the promise so server-renderer can wait on it
|
// return the promise so server-renderer can wait on it
|
||||||
|
|
Loading…
Reference in New Issue