Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use react-router lazy for dynamic route import #5790

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 20 additions & 44 deletions src/components/router/AsyncRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import loadable, { LoadableComponent } from '@loadable/component';
import React from 'react';
import type { RouteObject } from 'react-router-dom';

export enum AsyncRouteType {
Stable,
Expand All @@ -15,50 +14,27 @@ export interface AsyncRoute {
* Will fallback to using the `path` value if not specified.
*/
page?: string
/** The page element to render. */
element?: LoadableComponent<AsyncPageProps>
/** The page type used to load the correct page element. */
type?: AsyncRouteType
}

export interface AsyncPageProps {
/** The relative path to the page component in the routes directory. */
page: string
}

const DashboardAsyncPage = loadable(
(props: { page: string }) => import(/* webpackChunkName: "[request]" */ `../../apps/dashboard/routes/${props.page}`),
{ cacheKey: (props: AsyncPageProps) => props.page }
);

const ExperimentalAsyncPage = loadable(
(props: { page: string }) => import(/* webpackChunkName: "[request]" */ `../../apps/experimental/routes/${props.page}`),
{ cacheKey: (props: AsyncPageProps) => props.page }
);

const StableAsyncPage = loadable(
(props: { page: string }) => import(/* webpackChunkName: "[request]" */ `../../apps/stable/routes/${props.page}`),
{ cacheKey: (props: AsyncPageProps) => props.page }
);

export function toAsyncPageRoute({ path, page, element, type = AsyncRouteType.Stable }: AsyncRoute) {
let Element = element;
if (!Element) {
switch (type) {
case AsyncRouteType.Dashboard:
Element = DashboardAsyncPage;
break;
case AsyncRouteType.Experimental:
Element = ExperimentalAsyncPage;
break;
case AsyncRouteType.Stable:
default:
Element = StableAsyncPage;
}
const importPage = (page: string, type: AsyncRouteType) => {
switch (type) {
case AsyncRouteType.Dashboard:
return import(/* webpackChunkName: "[request]" */ `../../apps/dashboard/routes/${page}`);
case AsyncRouteType.Experimental:
return import(/* webpackChunkName: "[request]" */ `../../apps/experimental/routes/${page}`);
case AsyncRouteType.Stable:
return import(/* webpackChunkName: "[request]" */ `../../apps/stable/routes/${page}`);
}

return {
path,
element: <Element page={page ?? path} />
};
}
};

export const toAsyncPageRoute = ({ path, page, type = AsyncRouteType.Stable }: AsyncRoute): RouteObject => ({
path,
lazy: async () => {
const { default: Component } = await importPage(page ?? path, type);
return {
Component
};
}
});
Loading