Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support router lazy with preload
- Loading branch information
1 parent
9aafda9
commit d2e978c
Showing
10 changed files
with
131 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { useRoutes } from 'react-router-dom'; | ||
import { routes } from 'virtual:routes'; | ||
import { ReactElement, Suspense } from 'react'; | ||
|
||
export const Content = () => { | ||
export const Content = ({ fallback }: { fallback: ReactElement }) => { | ||
const routesElement = useRoutes(routes); | ||
return <>{routesElement}</>; | ||
return <Suspense fallback={fallback}>{routesElement}</Suspense>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ComponentType, forwardRef, lazy, useRef } from 'react'; | ||
import { PageModule } from 'shared/types'; | ||
|
||
export type PreloadableComponent<T extends ComponentType<any>> = T & { | ||
preload: () => Promise<PageModule<T>>; | ||
}; | ||
|
||
// Runtime code | ||
// Inspired by https://github.com/ianschmitz/react-lazy-with-preload/blob/master/src/index.ts | ||
export function lazyWithPreload<T extends ComponentType<any>>( | ||
factory: () => Promise<{ default: T }> | ||
): PreloadableComponent<T> { | ||
const ReactLazyComponent = lazy(factory); | ||
let PreloadedComponent: T | undefined; | ||
let factoryPromise: Promise<PageModule<T>> | undefined; | ||
|
||
const Component = forwardRef(function LazyWithPreload(props, ref) { | ||
const ComponentToRender = useRef(PreloadedComponent ?? ReactLazyComponent); | ||
const Element = ComponentToRender.current as React.ComponentPropsWithRef<T>; | ||
return <Element ref={ref} {...props} />; | ||
}); | ||
|
||
const LazyWithPreload = Component as any as PreloadableComponent<T>; | ||
|
||
LazyWithPreload.preload = () => { | ||
if (!factoryPromise) { | ||
factoryPromise = factory().then((mod) => { | ||
PreloadedComponent = mod.default; | ||
return mod; | ||
}); | ||
} | ||
|
||
return factoryPromise; | ||
}; | ||
|
||
return LazyWithPreload; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters