⚠
vite-plugin-ssr has been renamed Vike, see migration guide.

Abort

By using throw render() or throw redirect() you abort the rendering of the current page and render something else instead.

throw redirect() VS throw render()

While throw redirect() changes the URL, throw render() preserves it:

  • If a user goes to /admin and throw redirect('/login') is called, then the user will see the new URL /login in the browser's address bar.
  • If a user goes to /admin and throw render('/login') is called, then the user keeps seeing the same URL /admin in the browser's address bar.

In general, we recommend using throw render('/login') instead of throw redirect('/login') as it preserves the URL and, therefore, the user's intention. We further explain this techniue at Guides > Authentication > Login flow.

Debug

If throw redirect() or throw render() doesn't work:

  • Make sure throw redirect() / throw render() isn't swallowed.
    In developement, check your server logs for the following log. If this log is missing then this means that vite-plugin-ssr didn't receive the throw redirect() / throw render() exception: some other code is catching and swallowing it.
    10:00:00 AM [vite-plugin-ssr][request(42)] throw redirect('/some-url') intercepted while
    rendering /some-other-url
    

If throw redirect() doesn't work:

  • Make sure to add pageContext.httpResponse.headers to the HTTP response.
    If you've embedded vite-plugin-ssr into your server using renderPage(), then insepct whether pageContext.httpResponse.headers contains the Location header and double check that you're correctly adding all the headers defined by pageContext.httpResponse.headers to the HTTP response.