-
Hi, I don't want to lazy load child routes separately, I want them to be part of the parent route when that is been loaded lazily. Now when I try to do this, the child routes will be added to my main bundle. How could I structure my router so, that the child routes are bundled with the parent route? import Child1 from "./child-1";
import Child2 from "./child-2";
const Parent = lazy(() => import("./parent-route"));
export const routes: RouteObject[] = [
{
path: "parent",
element: <Parent />, // Lazyload
children: [
{ index: true, element: <Child1 />},
{
path: "child-2",
element: <Child2 />
},
],
},
]; |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
Perhaps the child routes should be imported directly in the parent route then. import Child1 from './child-1'
import Child2 from './child-2'
export default function Parent() {
// parent route
}
// expose child routes on Parent route object
Parent.Child1 = Child1
Parent.Child2 = Child2 const Parent = lazy(() => import("./parent-route"));
export const routes: RouteObject[] = [
{
path: "parent",
element: <Parent />, // Lazyload
children: [
{ index: true, element: <Parent.Child1 />},
{
path: "child-2",
element: <Parent.Child2 />
},
],
},
]; |
Beta Was this translation helpful? Give feedback.
-
@tkapitein Hello! Did you find a solution for that problem? |
Beta Was this translation helpful? Give feedback.
-
The question is what bundler you are using. In Webpack you define the chunk name and therefore which code snippets are put into a chunk.
Maybe a solution is to create a custom function customLazy( promise, mapper = {} ) {
const base = lazy( promise );
Object.entries( mapper ).forEach(([key, value]) => {
base[ key ] = lazy( () =>
promise.then((module) => ({
"default": module[ value ]
}))
);
};
return base;
}
const LazyParent = customLazy( import("./parent"), {
"Child1": "Child1",
"Child2": "Child2",
}); |
Beta Was this translation helpful? Give feedback.
-
Watched the Roadmap Planning session yesterday (https://www.youtube.com/watch?v=4Q1P8T27UBo) and I think #9884 will solve this question in a nicer way. I kind of hope preloading route components will be added as well. In our project we wrote a wrapper around React.lazy so we can preload the route components when we want to. |
Beta Was this translation helpful? Give feedback.
-
I found this one solution with With this approach, you can decide whether the child routes load lazily or not when the parent route loads. Here is the working example, function App() {
return (
<Routes>
<Route path="/">
<Route path="parent/*" element={<ParentRoute />} /> // 'parent/*' route name convention is necessary for parent route
</Route>
</Routes>
);
}
function ParentRoute() {
return (
<Routes>
<Route path="child1" element={<Child1 />} />
<Route path="child2" element={<Child2 />} />
</Routes>
);
} Note: With this approach, you have to specify full path to <NavLink to="parent/child1" /> |
Beta Was this translation helpful? Give feedback.
Watched the Roadmap Planning session yesterday (https://www.youtube.com/watch?v=4Q1P8T27UBo) and I think #9884 will solve this question in a nicer way. I kind of hope preloading route components will be added as well. In our project we wrote a wrapper around React.lazy so we can preload the route components when we want to.