frandiox / vite-ssr

Use Vite for server side rendering in Node
MIT License
823 stars 91 forks source link

Question: How to handle Errors #188

Open lautiamkok opened 1 year ago

lautiamkok commented 1 year ago

Is there any guide on how to handle errors, e.g. 500, on the server side? I still can't figure out how to do it with this plugin:

export default viteSSR(App, 
  { 
    routes 
  }, 
  ({ url, app, router, initialState, request, isClient, writeResponse, redirect }) => {
    const head = createHead()
    app.use(head)

    app.config.errorHandler = (err, vm, info) => {
      const statusCode = err.statusCode ? err.statusCode : 500

      // Reload browser so that the error is handled on SSR instead.
      if (isClient) {
         location.reload()
       }

       if (import.meta.env.SSR) {
         writeResponse({
           status: statusCode,
           html: err.message // how can we pass the app html here?
        })
       }
    }
    return { head }
  }
)

Thanks!