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(useId): ensure useId consistency when using serverPrefetch #12128

Merged
merged 2 commits into from
Oct 11, 2024
Merged
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
35 changes: 35 additions & 0 deletions packages/runtime-core/__tests__/helpers/useId.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
defineAsyncComponent,
defineComponent,
h,
onServerPrefetch,
useId,
} from 'vue'
import { renderToString } from '@vue/server-renderer'
Expand Down Expand Up @@ -145,6 +146,40 @@ describe('useId', () => {
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 () => {
const factory = (
delay1: number,
Expand Down
13 changes: 8 additions & 5 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,11 +856,10 @@ function setupStatefulComponent(
// 2. call setup()
const { setup } = Component
if (setup) {
pauseTracking()
const setupContext = (instance.setupContext =
setup.length > 1 ? createSetupContext(instance) : null)

const reset = setCurrentInstance(instance)
pauseTracking()
const setupResult = callWithErrorHandling(
setup,
instance,
Expand All @@ -870,12 +869,16 @@ function setupStatefulComponent(
setupContext,
],
)
const isAsyncSetup = isPromise(setupResult)
Copy link
Member

Choose a reason for hiding this comment

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

Tweaked the lines here so all the const declarations are together for better minification

resetTracking()
reset()

if (isPromise(setupResult)) {
// async setup, mark as async boundary for useId()
if (!isAsyncWrapper(instance)) markAsyncBoundary(instance)
if ((isAsyncSetup || instance.sp) && !isAsyncWrapper(instance)) {
// async setup / serverPrefetch, mark as async boundary for useId()
markAsyncBoundary(instance)
}

if (isAsyncSetup) {
setupResult.then(unsetCurrentInstance, unsetCurrentInstance)
if (isSSR) {
// return the promise so server-renderer can wait on it
Expand Down