⚠️
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 redirect()

See also:

// Redirect the user to another URL
redirect(url: `/${string}` | `https://${string}` | `http://${string}`)
import { redirect } from 'vite-plugin-ssr/abort'

function onSomeHook() {
  if (someCondition) {
    throw redirect('/some-url')
  }
}

throw redirect() makes temporary redirections (HTTP status code 302). For permanent redirections (HTTP status code 301), you can set config.redirects or pass a second argument throw redirect('/some-url', 301).

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

Common use cases:

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