Skip to content

Commit d9c1591

Browse files
committed
fix: bind existing hook to instance proxy
1 parent 81a7347 commit d9c1591

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

packages/runtime-core/src/apiLifecycle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export function onServerPrefetch<
108108
if (hook) {
109109
// Merge hook
110110
type.serverPrefetch = () =>
111-
Promise.all([handler(), (hook as Function)()])
111+
Promise.all([handler(), (hook as Function).call(target.proxy)])
112112
} else {
113113
type.serverPrefetch = handler
114114
}

packages/server-renderer/__tests__/render.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,5 +797,36 @@ function testRender(type: string, render: typeof renderToString) {
797797
expect(checkOther).toEqual([false, false])
798798
expect(done).toEqual([true, true])
799799
})
800+
801+
test('onServerPrefetch with serverPrefetch option', async () => {
802+
const msg = Promise.resolve('hello')
803+
const msg2 = Promise.resolve('hi')
804+
const app = createApp({
805+
data() {
806+
return {
807+
message: ''
808+
}
809+
},
810+
811+
async serverPrefetch() {
812+
this.message = await msg
813+
},
814+
815+
setup() {
816+
const message2 = ref('')
817+
onServerPrefetch(async () => {
818+
message2.value = await msg2
819+
})
820+
return {
821+
message2
822+
}
823+
},
824+
render() {
825+
return h('div', `${this.message} ${this.message2}`)
826+
}
827+
})
828+
const html = await render(app)
829+
expect(html).toBe(`<div>hello hi</div>`)
830+
})
800831
})
801832
}

0 commit comments

Comments
 (0)