T
TanStack•10mo ago
modern-teal

Redirect to external URL from server function 2

To avoid necroing the original thread, Tanner has suggested to return a 302 and redirect 'as normal' https://discord.com/channels/719702312431386674/1299757633758035998/1299766886631870474 But this in fact does not work, and instead you get
{"message":"Request failed with status 302","body":{"result":{"$undefined":""},"context":{}}}
{"message":"Request failed with status 302","body":{"result":{"$undefined":""},"context":{}}}
so idk how I am supposed to handle this now EDIT trying t call the server fn from the FE with useServerFn results in
ServerFn Response: 500
- Payload: {"code":"ERR_HTTP_HEADERS_SENT"}
ServerFn Response: 500
- Payload: {"code":"ERR_HTTP_HEADERS_SENT"}
but like, duh, ofc headers were sent thats how you redirect
20 Replies
modern-teal
modern-tealOP•10mo ago
OK update: The answer is to use sendRedirect from vinxi despite that being explicitly advised against
wise-white
wise-white•10mo ago
Maybe the veriage here needs to change? https://tanstack.com/router/latest/docs/framework/react/start/server-functions#redirects Its mentioned that sendRedirect shouldn't be used of 'soft redirects', whilst you are doing an external/hard redirect.
Server Functions | TanStack Router React Docs
What are Server Functions? Server functions allow you to specify logic that can be invoked anywhere (even the client), but run only on the server. In fact, they are not so different from an API route,...
modern-teal
modern-tealOP•10mo ago
I think that the distinction itself is poorly named, one is simulated navigation and performed clientside, and the other is a redirection. hard/soft suggest proper/improper semantics Even knowing how this works I fell for the naming
wise-white
wise-white•10mo ago
Suggestions here? Just internal/external redirection then?
modern-teal
modern-tealOP•10mo ago
basically, yes, imo at least also I just noticed, even with sendRedirect I get
DefaultCatchBoundary Error: Error: {"message":"Request failed with status 302","body":{"result":{"$undefined":""},"context":{}}}
DefaultCatchBoundary Error: Error: {"message":"Request failed with status 302","body":{"result":{"$undefined":""},"context":{}}}
which is a big issue, as I'm using the vinxi session to perform auth and the session does not seal for an error
wise-white
wise-white•10mo ago
This may have to do with the bug where server functions at the moment aren't accounting for users returning a Response.
wise-white
wise-white•10mo ago
GitHub
Start: createServerFn cannot return raw response objects · Issue #2...
Which project does this relate to? Start Describe the bug Server functions should able to return Raw Response objects. Your Example Website or App https://github.com/nekochan0122/tanstack-start-ret...
modern-teal
modern-tealOP•10mo ago
I am not returning a response, I'm returning null as I already encountered that issue lol
modern-teal
modern-tealOP•10mo ago
No description
wise-white
wise-white•10mo ago
https://github.com/TanStack/router/issues/2776 We've got one for that too 😅
GitHub
Start: createServerFn cannot return null · Issue #2776 · TanStack...
Which project does this relate to? Router Describe the bug All details are found in this discussion thread on Discord - https://discord.com/channels/719702312431386674/1306957125246914590 The crust...
modern-teal
modern-tealOP•10mo ago
LOL
wise-white
wise-white•10mo ago
Tanner is working on it
modern-teal
modern-tealOP•10mo ago
0 is also no good
modern-teal
modern-tealOP•10mo ago
No description
modern-teal
modern-tealOP•10mo ago
it's the sendRedirect itself thats fucky
wise-white
wise-white•10mo ago
Confirming by looking at the source of h3, sendRedirect is meant to be returned 'as-is'. We'll only be able to confirm this behaviour here once the server function bugs get squashed
modern-teal
modern-tealOP•10mo ago
Must have not thought that one through as theres nothing you can return from sendRedirect regardless
No description
modern-teal
modern-tealOP•10mo ago
redirections in general are not allowed to have bodies so this does not surprise me
modern-teal
modern-tealOP•10mo ago
Huh, I have a serverFn that is basically just returning the session, and it seemingly is fully able to do so
No description
modern-teal
modern-tealOP•10mo ago
but the body arrives as undefined OK the issue is that I'm calling session.update more than a single time which creates 2 Set-Cookie headers, corrupting my data

Did you find this page helpful?