1
1
mirror of https://github.com/aelve/guide.git synced 2024-11-29 06:23:17 +03:00
guide/front/build/serverRequestsHandler.ts
avele aeb9445613
Handling api requests errors, error toasts, server side rendering 404 page (#397)
* Error toasts, server side rendering 404 page

* Removed autohide from error toasts

* Correct response status  from server in case of error

* Api request error toast message changed

* Deepscan fixes
2019-09-14 19:40:16 +04:00

30 lines
731 B
TypeScript

// TODO add icon and refactor, change favicon serving
const urlsToSkip = [
'/favicon.ico'
]
export default async function handler (ctx, renderer) {
const { url } = ctx
// TODO add favicon skip favicon.
if (urlsToSkip.includes(url)) {
ctx.body = ''
return
}
try {
ctx.response.header['Content-Type'] = 'text/html'
ctx.body = await renderer.renderToString({ url })
} catch (error) {
const is404 = error.response && error.response.status === 404
if (is404) {
ctx.body = await renderer.renderToString({ url, is404 })
} else {
console.error('[Error] SSR render error:', error)
ctx.status = 500
ctx.body = error.message || 'Unknown Internal Server Error'
}
}
}