T
TanStack2mo ago
correct-apricot

Is it possible to access router.buildLocation() in a Server Route?

Did a cursory search for buildLocation, and no results returned...but is it possible to access buildLocation() (or something simillar) on a server route so that I can build a typesafe redirect url?
export const Route = createFileRoute("/(api)/api/auth/callback")({
validateSearch: z.object({
code: z.string().optional(),
next: z.string().optional(),
}),
server: {
handlers: {
GET: async ({ request }) => {

const url = XXX.buildLocation({ to: "/workspace/dashboard" })

// do business logic

return Response.redirect([PUT RESULT HERE], 302)
},
},
},
})
export const Route = createFileRoute("/(api)/api/auth/callback")({
validateSearch: z.object({
code: z.string().optional(),
next: z.string().optional(),
}),
server: {
handlers: {
GET: async ({ request }) => {

const url = XXX.buildLocation({ to: "/workspace/dashboard" })

// do business logic

return Response.redirect([PUT RESULT HERE], 302)
},
},
},
})
I can't seem to find a helper function at the moment, am I missing something or do I need to hardcode urls for server routes for now? Thanks a lot!
5 Replies
fair-rose
fair-rose2mo ago
hi, again, please ask in #start-questions and yes, you can do this
const router = getRouterInstance()
const href = router.buildLocation({ to: "/workspace/dashboard" }).href
const router = getRouterInstance()
const href = router.buildLocation({ to: "/workspace/dashboard" }).href
correct-apricot
correct-apricotOP2mo ago
oh for goodness sake, im sorry...again. I thought I was in the right one
fair-rose
fair-rose2mo ago
you could also just throw redirect({ to: "/workspace/dashboard" }) i think
correct-apricot
correct-apricotOP2mo ago
thank you for leading me to the helper method, I'll give the throw a shot too. Very much appreciated (confirmed throw redirect works)
fair-rose
fair-rose2mo ago
nice

Did you find this page helpful?