⚠️
The vite-plugin-ssr project has been renamed Vike.
  • If you are already using vite-plugin-ssr then migrate to Vike.
  • For new projects, don't use vite-plugin-ssr but use Vike instead.

throw render()

See also:

// Render the error page
render(abortStatusCode: 401 | 403 | 404 | 429 | 500 | 503, abortReason?: unknown)
// Render another page (aka URL rewrite)
render(url: `/${string}`, abortReason?: unknown)

If you use TypeScript, you can define the abortReason type by using the global interface Vike.PageContext, see API > _error.page.js.

import { render } from 'vite-plugin-ssr/abort'

function onSomeHook() {
  if (someCondition) {
    throw render('/login')
  }
  if (someOtherCondition) {
    throw render(401, "You don't have the permission to access this page.")
  }
}
  • If the first argument is a URL, then that URL is rendered.
  • If the first argument is a status code, then the error page is rendered.
  • The abortReason and abortStatusCode arguments are made available at pageContext.abortReason and pageContext.abortStatusCode which is useful for enabling the error page to show a useful error message to the user, see API > _error.page.js.

While it's most commonly used with guard() or onBeforeRender() you can use it with any hook.

Common use cases:

If throw render() doesn't work, see Abort > Debug.