vikejs / vike

🔨 Flexible, lean, community-driven, dependable, fast Vite-based frontend framework.
https://vike.dev
MIT License
4.36k stars 350 forks source link

Use `throw render()` / `throw redirect()` inside React/Vue/Solid components #1707

Open brillout opened 4 months ago

brillout commented 4 months ago

Description

In principle, it should be possible to use throw render() and throw redirect() inside UI components.

For this to work, vike-{react,vue,solid} need to catch the exception and then re-throw it inside the onRenderHtml()/onRenderClient() hook, so that the exception propagates to Vike.

Contribution welcome.

brillout commented 4 months ago

Deprioritizing, as per https://github.com/vikejs/vike-react/pull/123#issuecomment-2196236446.

simplecommerce commented 1 month ago

Is this not supposed to work already? In my test project, I implemented vike-node and am using throw redirect() in a React Component I have, but only in SSR in order to redirect to another page when the user isn't logged-in, and it seems to work fine.

brillout commented 1 month ago

@simplecommerce Yes it already works if the onRenderHtml() and onRenderClient() hooks receive the exception thrown by throw redirect() / throw render(), and if you aren't using HTML streaming. Because it isn't clear what should happen when throw redirect() / throw render() is called during HTML Streaming (will browsers respect the redirect header after the HTML stream has started?), I recommend defining user redirections on a page level instead of a component level.