Subscribe on changes!

renderToString not throwing error from onServerPrefetch

avatar
Aug 29th 2023

Vue version

3.3.4

Link to minimal reproduction

import { h, createApp, onServerPrefetch } from 'vue'
import { renderToString } from 'vue/server-renderer'

const Child = {
    render() {
        return h('span')
    },
    setup() {
        onServerPrefetch(async () => {
            throw new Error('An error')
        })
    }
}

const app = createApp({
    render () {
        return h('div', h(Child))
    }
})

try {
    await renderToString(app)
}
catch (e) {
    console.log(e.message);
}

Steps to reproduce

Run the script

What is expected?

The output is An error

What is actually happening?

file:///.../issue.js:10
            throw new Error('An error')
                  ^

Error: An error
    at file:///.../issue.js:10:19
    at /.../node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:2669:88
    at callWithErrorHandling (/.../node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:156:18)
    at callWithAsyncErrorHandling (/.../node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:164:17)
    at hook.__weh.hook.__weh (/.../node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:2649:19)
    at /.../node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:625:65
    at Array.map (<anonymous>)
    at /.../node_modules/@vue/server-renderer/dist/server-renderer.cjs.js:625:38

System Info

No response

Any additional comments?

No response

avatar
Nov 27th 2023

It will cause my server code to crash, I need to write a wrapper function to use it, like:

export function safeOnServerPrefetch(fn: () => any) {
    onServerPrefetch(async () => {
        try {
            await fn();
        } catch (e) {
            console.log('Running onServerPrefetch error:', e);
        }
    });
}