Skip to content

Commit b4d3534

Browse files
authoredOct 11, 2024··
fix(useId): ensure useId consistency when using serverPrefetch (#12128)
close #12102
1 parent ec917cf commit b4d3534

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed
 

‎packages/runtime-core/__tests__/helpers/useId.spec.ts

+35
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
defineAsyncComponent,
99
defineComponent,
1010
h,
11+
onServerPrefetch,
1112
useId,
1213
} from 'vue'
1314
import { renderToString } from '@vue/server-renderer'
@@ -145,6 +146,40 @@ describe('useId', () => {
145146
expect(await getOutput(() => factory(16, 0))).toBe(expected)
146147
})
147148

149+
test('components with serverPrefetch', async () => {
150+
const factory = (): ReturnType<TestCaseFactory> => {
151+
const SPOne = defineComponent({
152+
setup() {
153+
onServerPrefetch(() => {})
154+
return () => h(BasicComponentWithUseId)
155+
},
156+
})
157+
158+
const SPTwo = defineComponent({
159+
render() {
160+
return h(BasicComponentWithUseId)
161+
},
162+
})
163+
164+
const app = createApp({
165+
setup() {
166+
const id1 = useId()
167+
const id2 = useId()
168+
return () => [id1, ' ', id2, ' ', h(SPOne), ' ', h(SPTwo)]
169+
},
170+
})
171+
return [app, []]
172+
}
173+
174+
const expected =
175+
'v-0 v-1 ' + // root
176+
'v-0-0 v-0-1 ' + // inside first async subtree
177+
'v-2 v-3' // inside second async subtree
178+
// assert different async resolution order does not affect id stable-ness
179+
expect(await getOutput(() => factory())).toBe(expected)
180+
expect(await getOutput(() => factory())).toBe(expected)
181+
})
182+
148183
test('async setup()', async () => {
149184
const factory = (
150185
delay1: number,

‎packages/runtime-core/src/component.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -856,11 +856,10 @@ function setupStatefulComponent(
856856
// 2. call setup()
857857
const { setup } = Component
858858
if (setup) {
859+
pauseTracking()
859860
const setupContext = (instance.setupContext =
860861
setup.length > 1 ? createSetupContext(instance) : null)
861-
862862
const reset = setCurrentInstance(instance)
863-
pauseTracking()
864863
const setupResult = callWithErrorHandling(
865864
setup,
866865
instance,
@@ -870,12 +869,16 @@ function setupStatefulComponent(
870869
setupContext,
871870
],
872871
)
872+
const isAsyncSetup = isPromise(setupResult)
873873
resetTracking()
874874
reset()
875875

876-
if (isPromise(setupResult)) {
877-
// async setup, mark as async boundary for useId()
878-
if (!isAsyncWrapper(instance)) markAsyncBoundary(instance)
876+
if ((isAsyncSetup || instance.sp) && !isAsyncWrapper(instance)) {
877+
// async setup / serverPrefetch, mark as async boundary for useId()
878+
markAsyncBoundary(instance)
879+
}
880+
881+
if (isAsyncSetup) {
879882
setupResult.then(unsetCurrentInstance, unsetCurrentInstance)
880883
if (isSSR) {
881884
// return the promise so server-renderer can wait on it

0 commit comments

Comments
 (0)
Please sign in to comment.