| import { Suspense, Fragment, lazy } from 'react' |
| import { BailoutToCSR } from './dynamic-bailout-to-csr' |
| import type { ComponentModule } from './types' |
| import { PreloadChunks } from './preload-chunks' |
|
|
| |
| |
| |
| function convertModule<P>( |
| mod: React.ComponentType<P> | ComponentModule<P> | undefined |
| ): { |
| default: React.ComponentType<P> |
| } { |
| |
| |
| |
| |
| |
| |
| const hasDefault = mod && 'default' in mod |
| return { |
| default: hasDefault |
| ? (mod as ComponentModule<P>).default |
| : (mod as React.ComponentType<P>), |
| } |
| } |
|
|
| const defaultOptions = { |
| loader: () => Promise.resolve(convertModule(() => null)), |
| loading: null, |
| ssr: true, |
| } |
|
|
| interface LoadableOptions { |
| loader?: () => Promise<React.ComponentType<any> | ComponentModule<any>> |
| loading?: React.ComponentType<any> | null |
| ssr?: boolean |
| modules?: string[] |
| } |
|
|
| function Loadable(options: LoadableOptions) { |
| const opts = { ...defaultOptions, ...options } |
| const Lazy = lazy(() => opts.loader().then(convertModule)) |
| const Loading = opts.loading |
|
|
| function LoadableComponent(props: any) { |
| const fallbackElement = Loading ? ( |
| <Loading isLoading={true} pastDelay={true} error={null} /> |
| ) : null |
|
|
| |
| const hasSuspenseBoundary = !opts.ssr || !!opts.loading |
| const Wrap = hasSuspenseBoundary ? Suspense : Fragment |
| const wrapProps = hasSuspenseBoundary ? { fallback: fallbackElement } : {} |
| const children = opts.ssr ? ( |
| <> |
| {/* During SSR, we need to preload the CSS from the dynamic component to avoid flash of unstyled content */} |
| {typeof window === 'undefined' ? ( |
| <PreloadChunks moduleIds={opts.modules} /> |
| ) : null} |
| <Lazy {...props} /> |
| </> |
| ) : ( |
| <BailoutToCSR reason="next/dynamic"> |
| <Lazy {...props} /> |
| </BailoutToCSR> |
| ) |
|
|
| return <Wrap {...wrapProps}>{children}</Wrap> |
| } |
|
|
| LoadableComponent.displayName = 'LoadableComponent' |
|
|
| return LoadableComponent |
| } |
|
|
| export default Loadable |
|
|