recommended way to redirect from api route?
i don't see any obvious way to do this. best thing i've come up with thus far without causing a 503 and/or just not executing the redirect is
is there any way to do this from within the start api itself?
15 Replies
modern-teal•7mo ago
i dont think we have any helper for that. you could maybe use https://h3.unjs.io/utils/response#sendredirectevent-location-code
Response - h3
Utilities to send response headers and data
robust-apricotOP•7mo ago
Ok thanks. Sorry, I've read the docs but there's a lot of stuff going on beneath the hood- where does h3 fit into this?
vite => vinxi => nitro => ?? => tanstack router => start?
or something like this
modern-teal•7mo ago
start uses nitro which uses h3
but sending a response as you did above looks perfectly fine to me
robust-apricotOP•7mo ago
yea i mostly just wanted to get typesafety like i can in serverFn's, but that's fine no big deal
modern-teal•7mo ago
inside of an API route you deal with "low level" stuff such as response, so that's ok there I'd say
typesafety in which way?
robust-apricotOP•7mo ago
i guess typesafety isn't the right word, type inference rather
modern-teal•7mo ago
but for what?
robust-apricotOP•7mo ago
for the route to redirect to
modern-teal•7mo ago
what would you redirect to?
a route that is handled by router/start?
robust-apricotOP•7mo ago
yea i'm redirecting to my root route basically
/
modern-teal•7mo ago
hm quite the niche use case i'd say. if you need to redirect to other routes than
/
(with typesafety) you could extract the route paths from the route treerobust-apricotOP•7mo ago
Yea it's not really a big deal
it's mostly a matter of, an upside of the router is getting inference everywhere, and always falling back to a start/router api to do things like this
so it feels odd to have this one case (api routes) where it's not possible
but niche nonetheless
environmental-rose•7mo ago
yeah i had that issue too. Needed to redirect to a route handled by Start with some search params. It gets quite messy 😅
yappiest-sapphire•7mo ago
Yeah me too
mute-gold•6mo ago
I'm facing this case with supabase auth. When a user register, we want to confirm the email, we need to redirect to an api route. So I'm doing something like this:
It works, but I don't know if its right. but I think it could be good to be able to use
redirect
from tanstack
. in other framework it seems to be possible (next - RR7)
I can also do this in a server function but I think api route is the way to go (at least this is how they do in supabase docs in next)