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(server-renderer): Fix call to serverPrefetch in server renderer with an async setup #10893

20 changes: 20 additions & 0 deletions packages/server-renderer/__tests__/render.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,26 @@ function testRender(type: string, render: typeof renderToString) {
expect(html).toBe(`<div>hello</div>`)
})

test('serverPrefetch w/ async setup', async () => {
const msg = Promise.resolve('hello')
const app = createApp({
data() {
return {
msg: '',
}
},
async serverPrefetch() {
this.msg = await msg
},
render() {
return h('div', this.msg)
},
async setup() {},
})
const html = await render(app)
expect(html).toBe(`<div>hello</div>`)
})

// #2763
test('error handling w/ async setup', async () => {
const fn = vi.fn()
Expand Down
25 changes: 12 additions & 13 deletions packages/server-renderer/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,20 @@ export function renderComponentVNode(
const instance = createComponentInstance(vnode, parentComponent, null)
const res = setupComponent(instance, true /* isSSR */)
const hasAsyncSetup = isPromise(res)
const prefetches = instance.sp /* LifecycleHooks.SERVER_PREFETCH */
let prefetches = instance.sp /* LifecycleHooks.SERVER_PREFETCH */
if (hasAsyncSetup || prefetches) {
let p: Promise<unknown> = hasAsyncSetup
? (res as Promise<void>)
: Promise.resolve()
if (prefetches) {
p = p
.then(() =>
Promise.all(
const p: Promise<unknown> = Promise.resolve(res as Promise<void>)
.then(() => {
// instance.sp may be null until an async setup resolves, so evaluate it here
prefetches = instance.sp
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
prefetches = instance.sp
if(hasAsyncSetup) prefetches = instance.sp

Copy link
Member

Choose a reason for hiding this comment

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

I still believe here should check hasAsyncSetup

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please see commit 7bf6732

if (prefetches) {
return Promise.all(
prefetches.map(prefetch => prefetch.call(instance.proxy)),
),
)
// Note: error display is already done by the wrapped lifecycle hook function.
.catch(NOOP)
}
)
}
})
// Note: error display is already done by the wrapped lifecycle hook function.
.catch(NOOP)
return p.then(() => renderComponentSubTree(instance, slotScopeId))
} else {
return renderComponentSubTree(instance, slotScopeId)
Expand Down