TanStackT
TanStack3mo ago
6 replies
foolish-indigo

Optional locale param with wildcard

I have /{$-lang}/$ -like routes.
When user access /hello page, how would router know that "hello" is not a lang? Do I need to manually check and somehow redirect to the splat route?
I tried to do:
export const Route = createFileRoute("/_main/{-$lang}/")({
  component: Index,

  beforeLoad: ({ params }) => {
    // if there some prefix that is not a lang, redirect to splat route, becaue that is document id.
    if (!!params.lang && !i18n.locales.includes(params.lang as Locale)) {
      throw redirect({
        to:  "/{-$lang}/$",
        params: { lang: undefined, _splat: params.lang },
      });
    }
  },

but that would send the server into infinite loop.
Was this page helpful?