-
So if I have a parent route such as The issue is that if you immediately // dashboard.tsx
import type { LoaderFunction } from "@remix-run/node";
import { Outlet } from "@remix-run/react";
import { redirect } from "@remix-run/node";
import Box from "@mui/material/Box";
export const loader: LoaderFunction = async () => {
// This causes an infinite redirect loop
return redirect(`/dashboard/plans`);
};
export default function Dashboard() {
return (
<>
<Box>
<h1>Dashboard</h1>
<Outlet />
</Box>
</>
);
} What is the appropriate solution for this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
The issue is that when you navigate to a nested route like // dashboard.tsx
export const loader: LoaderFunction = async ({ request }) => {
const url = new URL(request.url);
if (url.pathname === "/dashboard") {
return redirect("/dashboard/plans");
}
return null;
}; Here, we're only redirecting if the current URL is So initial request is: Then after redirect, you get: So by checking the URL pathname, you'll only redirect on the initial request. EDIT: Make sure to checkout the comments below. It's preferable to redirect from an index route instead of a layout route. |
Beta Was this translation helpful? Give feedback.
The issue is that when you navigate to a nested route like
dashboard/plans
, Remix is calling the parent loaderdashboard
in addition to thedashboard/plans
loader. Since the parent is returning redirect, it gets caught in a loop. Perhaps Remix should detect this, but for now, here's a workaround:Here, we're only redirecting if the current URL is
/dashboard
. When Remix calls loaders, it uses the current route and identifies the route id with?data=route-id
So initial request…