T
TanStack•2y ago
compatible-crimson

Is there any obvious problem that you can see?

When I go to lets say /login or /notes nothing is loading even when user is authenticated.
const root = createRoot(document.getElementById("root") as HTMLElement);

const queryClient = new QueryClient();

const RootComponent = () => {
return (
<>
<Outlet />
{/* <ReactQueryDevtools buttonPosition="top-right" /> */}
<TanStackRouterDevtools position="bottom-right" />
</>
);
};

const rootRoute = rootRouteWithContext<{
queryClient: QueryClient;
}>()({
component: RootComponent,
beforeLoad: async ({ location }) => {
const { data } = await supabase.auth.getSession();

if (
location.pathname === "/login" ||
location.pathname === "/register" ||
location.pathname === "/"
) {
return;
}

if (!data?.session?.user) {
throw redirect({
to: "/login",
search: {
redirect: location.href,
},
});
}
},
});

const indexRoute = new Route({
getParentRoute: () => rootRoute,
path: "/",
component: WorkoutsPage,
});

const loginRoute = new Route({
getParentRoute: () => indexRoute,
path: "/login",
component: LoginPage,
});
const notesRoute = new Route({
getParentRoute: () => indexRoute,
path: "/notes",
component: NotesPage,
});
const workoutRoute = new Route({
getParentRoute: () => indexRoute,
path: "/workout/$id",
component: WorkoutPage,
});

const routeTree = rootRoute.addChildren([
indexRoute,
workoutRoute,
loginRoute,
notesRoute,
]);

const router = new Router({
routeTree,
defaultPreload: "intent",
defaultPreloadStaleTime: 0,
context: {
queryClient,
},
});

declare module "@tanstack/react-router" {
interface Register {
router: typeof router;
}
}

root.render(
<StrictMode>
<QueryClientProvider client={queryClient}>
<Toaster />
<RouterProvider router={router} />
</QueryClientProvider>
</StrictMode>,
);
const root = createRoot(document.getElementById("root") as HTMLElement);

const queryClient = new QueryClient();

const RootComponent = () => {
return (
<>
<Outlet />
{/* <ReactQueryDevtools buttonPosition="top-right" /> */}
<TanStackRouterDevtools position="bottom-right" />
</>
);
};

const rootRoute = rootRouteWithContext<{
queryClient: QueryClient;
}>()({
component: RootComponent,
beforeLoad: async ({ location }) => {
const { data } = await supabase.auth.getSession();

if (
location.pathname === "/login" ||
location.pathname === "/register" ||
location.pathname === "/"
) {
return;
}

if (!data?.session?.user) {
throw redirect({
to: "/login",
search: {
redirect: location.href,
},
});
}
},
});

const indexRoute = new Route({
getParentRoute: () => rootRoute,
path: "/",
component: WorkoutsPage,
});

const loginRoute = new Route({
getParentRoute: () => indexRoute,
path: "/login",
component: LoginPage,
});
const notesRoute = new Route({
getParentRoute: () => indexRoute,
path: "/notes",
component: NotesPage,
});
const workoutRoute = new Route({
getParentRoute: () => indexRoute,
path: "/workout/$id",
component: WorkoutPage,
});

const routeTree = rootRoute.addChildren([
indexRoute,
workoutRoute,
loginRoute,
notesRoute,
]);

const router = new Router({
routeTree,
defaultPreload: "intent",
defaultPreloadStaleTime: 0,
context: {
queryClient,
},
});

declare module "@tanstack/react-router" {
interface Register {
router: typeof router;
}
}

root.render(
<StrictMode>
<QueryClientProvider client={queryClient}>
<Toaster />
<RouterProvider router={router} />
</QueryClientProvider>
</StrictMode>,
);
1 Reply
compatible-crimson
compatible-crimsonOP•2y ago
Solution. I got confused with index routes and rootRoutes and it turns out you have to use rootRoute not indexRoute in route defining 😄

Did you find this page helpful?