2023-05-01 16:50:41 -04:00
|
|
|
import loadable from '@loadable/component';
|
2022-11-01 16:41:09 -04:00
|
|
|
import React from 'react';
|
|
|
|
import { Route } from 'react-router-dom';
|
|
|
|
|
2023-05-01 16:50:41 -04:00
|
|
|
export enum AsyncRouteType {
|
|
|
|
Stable,
|
|
|
|
Experimental
|
|
|
|
}
|
2022-11-01 16:41:09 -04:00
|
|
|
|
|
|
|
export interface AsyncRoute {
|
|
|
|
/** The URL path for this route. */
|
|
|
|
path: string
|
|
|
|
/** The relative path to the page component in the routes directory. */
|
|
|
|
page: string
|
2023-05-01 16:50:41 -04:00
|
|
|
/** The route should use the page component from the experimental app. */
|
|
|
|
type?: AsyncRouteType
|
|
|
|
}
|
|
|
|
|
|
|
|
interface AsyncPageProps {
|
|
|
|
/** The relative path to the page component in the routes directory. */
|
|
|
|
page: string
|
2022-11-01 16:41:09 -04:00
|
|
|
}
|
|
|
|
|
2023-05-01 16:50:41 -04:00
|
|
|
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 const toAsyncPageRoute = ({ path, page, type = AsyncRouteType.Stable }: AsyncRoute) => (
|
2022-11-01 16:41:09 -04:00
|
|
|
<Route
|
2023-05-01 16:50:41 -04:00
|
|
|
key={path}
|
|
|
|
path={path}
|
|
|
|
element={(
|
|
|
|
type === AsyncRouteType.Experimental ?
|
|
|
|
<ExperimentalAsyncPage page={page} /> :
|
|
|
|
<StableAsyncPage page={page} />
|
|
|
|
)}
|
2022-11-01 16:41:09 -04:00
|
|
|
/>
|
|
|
|
);
|