import React, { lazy, Suspense } from 'react' import Loadable from './loadable' import NoSSR from './dynamic-no-ssr' type ComponentModule
= { default: React.ComponentType
} export type LoaderComponent
= Promise = () => LoaderComponent
export type LoaderMap = { [module: string]: () => Loader = LoadableGeneratedOptions & {
loading?: (loadingProps: DynamicOptionsLoadingProps) => JSX.Element | null
loader?: Loader | LoaderMap
loadableGenerated?: LoadableGeneratedOptions
ssr?: boolean
/**
* @deprecated `suspense` prop is not required anymore
*/
suspense?: boolean
}
export type LoadableOptions = DynamicOptions
export type LoadableFn = (
opts: LoadableOptions
) => React.ComponentType
export type LoadableComponent = React.ComponentType
export function noSSR (
LoadableInitializer: Loader,
loadableOptions: DynamicOptions
): React.ComponentType {
// Removing webpack and modules means react-loadable won't try preloading
delete loadableOptions.webpack
delete loadableOptions.modules
const NoSSRComponent = lazy(LoadableInitializer)
const Loading = loadableOptions.loading!
const fallback = (
(
dynamicOptions: DynamicOptions | Loader ,
options?: DynamicOptions
): React.ComponentType {
let loadableFn: LoadableFn = Loadable
let loadableOptions: LoadableOptions = {
// A loading component is not required, so we default it
loading: ({ error, isLoading, pastDelay }) => {
if (!pastDelay) return null
if (process.env.NODE_ENV !== 'production') {
if (isLoading) {
return null
}
if (error) {
return (
{error.message}
Loading something
const loader = () => loaderFn().then(convertModule)
// coming from build/babel/plugins/react-loadable-plugin.js
if (loadableOptions.loadableGenerated) {
loadableOptions = {
...loadableOptions,
...loadableOptions.loadableGenerated,
loader,
}
delete loadableOptions.loadableGenerated
}
// support for disabling server side rendering, eg: dynamic(() => import('../hello-world'), {ssr: false}).
if (typeof loadableOptions.ssr === 'boolean') {
if (!loadableOptions.ssr) {
delete loadableOptions.ssr
return noSSR(loader as Loader, loadableOptions)
}
delete loadableOptions.ssr
}
return loadableFn(loadableOptions)
}
{error.stack}