Skip to content

Commit c8eda17

Browse files
yangxiuxiu1115abdullah-wn
authored andcommitted
fix(useId): ensure useId consistency when using serverPrefetch (vuejs#12128)
close vuejs#12102
1 parent 21a0396 commit c8eda17

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

Diff for: 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,

Diff for: packages/runtime-core/src/component.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -858,11 +858,10 @@ function setupStatefulComponent(
858858
// 2. call setup()
859859
const { setup } = Component
860860
if (setup) {
861+
pauseTracking()
861862
const setupContext = (instance.setupContext =
862863
setup.length > 1 ? createSetupContext(instance) : null)
863-
864864
const reset = setCurrentInstance(instance)
865-
pauseTracking()
866865
const setupResult = callWithErrorHandling(
867866
setup,
868867
instance,
@@ -872,12 +871,16 @@ function setupStatefulComponent(
872871
setupContext,
873872
],
874873
)
874+
const isAsyncSetup = isPromise(setupResult)
875875
resetTracking()
876876
reset()
877877

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

0 commit comments

Comments
 (0)