Skip to content

Commit 1403bce

Browse files
Akryumyyx990803
authored andcommitted
fix: bind existing hook to instance proxy
1 parent 977ffd0 commit 1403bce

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
@@ -941,5 +941,36 @@ function testRender(type: string, render: typeof renderToString) {
941941
expect(checkOther).toEqual([false, false])
942942
expect(done).toEqual([true, true])
943943
})
944+
945+
test('onServerPrefetch with serverPrefetch option', async () => {
946+
const msg = Promise.resolve('hello')
947+
const msg2 = Promise.resolve('hi')
948+
const app = createApp({
949+
data() {
950+
return {
951+
message: ''
952+
}
953+
},
954+
955+
async serverPrefetch() {
956+
this.message = await msg
957+
},
958+
959+
setup() {
960+
const message2 = ref('')
961+
onServerPrefetch(async () => {
962+
message2.value = await msg2
963+
})
964+
return {
965+
message2
966+
}
967+
},
968+
render() {
969+
return h('div', `${this.message} ${this.message2}`)
970+
}
971+
})
972+
const html = await render(app)
973+
expect(html).toBe(`<div>hello hi</div>`)
974+
})
944975
})
945976
}

0 commit comments

Comments
 (0)