Question
I'm using Tanstack Router and Tanstack Start and I'm wondering if there's a way to do something like:
const path = buildPath({ to: "/$lang/posts/$id", params: { lang, id }});
so that path
is the resolved string and buildPath
only accepts type safe to
and params
.
Specific use case
I'm using OAuth as an authentication method and I have an api route at /api/auth/google
which accepts a redirect
query param so that once the authentication is complete the flow ends by redirecting to redirect
and I'd like to pass a valid path to this redirect
query param like so:
const path = buildPath({ to: "/$lang/posts/$id", params: { lang, id }});
// ...
<a href={`/api/auth/google?redirect=${encodeURIComponent(path)}`}>...</a>
Question
I'm using Tanstack Router and Tanstack Start and I'm wondering if there's a way to do something like:
const path = buildPath({ to: "/$lang/posts/$id", params: { lang, id }});
so that path
is the resolved string and buildPath
only accepts type safe to
and params
.
Specific use case
I'm using OAuth as an authentication method and I have an api route at /api/auth/google
which accepts a redirect
query param so that once the authentication is complete the flow ends by redirecting to redirect
and I'd like to pass a valid path to this redirect
query param like so:
const path = buildPath({ to: "/$lang/posts/$id", params: { lang, id }});
// ...
<a href={`/api/auth/google?redirect=${encodeURIComponent(path)}`}>...</a>
Share
Improve this question
edited Mar 10 at 9:43
Shoe
asked Mar 9 at 16:04
ShoeShoe
76.3k38 gold badges176 silver badges278 bronze badges
4
|
1 Answer
Reset to default 0You can build a pathname using:
const router = useRouter();
and then:
router.buildLocation({ to: "...", params: { ... }, search: { ... }});
this will return a string that can be used anywhere.
Credits to Manuel Schiller from the Tanstack Discord Server.
buildPath
? I don't see any reference to it in the Tanstack documentation or repo. Can you edit to include a complete minimal reproducible example and link to any reference/documentation material you are working from? – Drew Reese Commented Mar 9 at 18:38{ to: ..., params: ... }
(the props you pass to a<Link>
component) and returns a string with the URL path that can be built with those props. Anyway I found my answer in the discord server and cross posted it here. Thanks. – Shoe Commented Mar 10 at 9:42