Best way to batch lazy load multiple routes at the same time? #10322
Replies: 2 comments
-
Right now, data routers require you to define the entire routing tree up front, since we need to know about those routes prior to rendering so we can match and call the appropriate loaders (in order to decouple data fetching from rendering). As you noted you could use Eventually we do plan to extend the API so that you can lazily define |
Beta Was this translation helpful? Give feedback.
-
Here's the solution I made to batch load several routes. BatchLazy.tsximport React from 'react';
import {Outlet} from "react-router-dom";
// Do not lazy load here
import FooView from "@views/FooView.tsx";
import BarView from "@views/BarView.tsx";
const BatchLazy = () => <Outlet />
// Export them or they'll be stripped off
export {FooView, BarView}
export default BatchLazy routes.tsxconst BatchLazy = lazy(() => import('@config/routes/BatchLazy.tsx'));
// Lazy load `FooView` and `BarView` only so you can reference them in the routes
// By the time you open it they would have already been loaded by BatchLazy anyway
const FooView = lazy(() => import('@views/FooView.tsx'));
const BarView = lazy(() => import('@views/BarView.tsx'));
export const routes = (isAuth: boolean) => createBrowserRouter([
{path: '/', element: (
<Suspense fallback={'Loading...'}>
<BatchLazy />
</Suspense>
), children: [
{path: 'foo', Component: FooView},
{path: 'bar', Component: BarView},
]},
]); It works for me but I sure wish there was a build-in way to do this using v6. |
Beta Was this translation helpful? Give feedback.
-
In
v5
you would do something like this:I'm using the new data routers, and I'm trying to minimize my usage of nested
Routes
which can be automatically slotted in for the approach I detailed above. I have read this section on one possible implementation, but that's nothing close to a 1:1. Are there any approaches I could use that both minimize my use of nestedRoutes
and don't require me to create a significant new convention in our project?Beta Was this translation helpful? Give feedback.
All reactions