TanStackT
TanStack3mo ago
12 replies
rubber-blue

Locale Loader Runs on Auth Routes

I'm having an issue with TanStack Router where my locale route's loader is being triggered on auth routes like /auth/sign-in, causing unwanted API calls and 401 errors

Current structure:
- Route file: {$locale}/route.tsx
- Auth routes: (authentication)/auth/sign-in.tsx

Expected behavior:
- /en or /tr should run the locale loader and fetch menus
- /auth/sign-in should NOT run the locale loader

What I've tried:
1. Changed from {-$locale} to {$locale} to make it non-optionals
3. Put auth routes in a separate (authentication) directory

What am I missing? How do I properly configure TanStack Router to prevent the locale loader from running on auth routes?




locale/route.tsx:
import { createFileRoute, Outlet } from "@tanstack/react-router";
import { IntlayerProvider, useLocale } from "react-intlayer";
import {
  getGetApiCmsMenusCmsMenuGetAllWithParentQueryOptions,
  useGetApiCmsMenusCmsMenuGetAllWithParent,
} from "@/api/generated/endpoints/cms-menus/cms-menus";
import { getApiCmsMenusCmsMenuGetAllWithParentIdParentIdParams } from "@/api/generated/endpoints/cms-menus/cms-menus.zod";
import { useI18nHTMLAttributes } from "@/hooks/useI18nHTMLAttributes";

export const Route = createFileRoute("/{$locale}")({
  loader: async ({ context, params }) => {
    console.log(`LOADER - Locale: ${params.locale}`);
    const queryOptions = getGetApiCmsMenusCmsMenuGetAllWithParentQueryOptions();
    await context.queryClient.ensureQueryData(queryOptions);
    return null;
  },
  component: LayoutComponent,
});

function LayoutComponent() {
  useI18nHTMLAttributes();

  const { defaultLocale } = useLocale();
  const { locale } = Route.useParams();

  const { data } = useGetApiCmsMenusCmsMenuGetAllWithParent();
  const parsed =
    getApiCmsMenusCmsMenuGetAllWithParentIdParentIdParams.safeParse(data);

  return (
    <IntlayerProvider locale={locale ?? defaultLocale}>
      <nav>
        <ul>
          {parsed.success
            ? parsed.data.map((item, idx) => (
                <li key={(item as any)?.id ?? idx}>
                  {item.displayName || item.name || "Menu"}
                </li>
              ))
            : null}
        </ul>
      </nav>
      <Outlet />
    </IntlayerProvider>
  );
}
image.png
Was this page helpful?