TanStackT
TanStack10mo ago
2 replies
then-purple

Programmatically get path in `__root.tsx`?

I'd like to get the current URL path within my __root.tsx. Is this possible?

My goal is to define my canonical URL in my head's links::
{
  rel: "canonical",
  href: `${import.meta.env.PUBLIC_ORIGIN}${pathname????}`,
},


Solutions considered:
- Specify this per route to override the head -- I don't want to do this, for this property, b/c not DRY.
- window.location.pathname -- won't work b/c of SSR
- ctx - the path is not available on the ctx unfortunately.
Screenshot_2025-04-07_at_10.10.02_PM.png
Was this page helpful?