vike: `throw Redirect()`

Description

Itโ€™s arleady possible with https://vite-plugin-ssr.com/RenderErrorPage#redirection but the DX should be polished.

We should:

  • Create a second utility throw Redirect()
  • Make pageContext.redirectTo a built-in property
  • Update all server integration examples
  • Automatically redirect /some/url/ to /some/url

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 9
  • Comments: 16 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Quite a tempting alternative:

import { redirect } from 'vite-plugin-ssr'

function onSomeHook() {
    throw redirect(307, '/login')
}
import { render } from 'vite-plugin-ssr'

function onSomeHook() {
    throw render(401, 'not logged in')
}

function onSomeHook() {
    throw render('/login')
}

But letโ€™s see if it works out. (There are some slight argument conflicts.)

Done:

Leaving this open until permanent redirections with config.redirects are implemented.

How about this then:

I think this us a good compromise! Keeps the API simple but allows full control for those who are in need ๐Ÿ‘๐Ÿผ

@brillout ๐Ÿ‘‹๐Ÿผ

How about we remove the statusCode option from throw redirect()?

I think there could be an argument for having 302 and 301/308 and therefore, Iโ€™d be in favor to keep them. Example: a very content-driven website wants to indicate whether something has been moved temporarily and permanently based on query params or the content itself.

I also think there is little overhead in this and making the arguments explicit even helps, because otherwise me as a developer would start to check the documentation to understand which status code is actually used.