/
dynamic.tsx
158 lines (135 loc) · 4.7 KB
/
dynamic.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React from 'react'
import Loadable from './loadable'
const isServerSide = typeof window === 'undefined'
export type LoaderComponent<P = {}> = Promise<
React.ComponentType<P> | { default: React.ComponentType<P> }
>
export type Loader<P = {}> = (() => LoaderComponent<P>) | LoaderComponent<P>
export type LoaderMap = { [mdule: string]: () => Loader<any> }
export type LoadableGeneratedOptions = {
webpack?(): any
modules?(): LoaderMap
}
export type LoadableBaseOptions<P = {}> = LoadableGeneratedOptions & {
loading?: ({
error,
isLoading,
pastDelay,
}: {
error?: Error | null
isLoading?: boolean
pastDelay?: boolean
retry?: () => void
timedOut?: boolean
}) => JSX.Element | null
loader?:
| Loader<P>
| LoaderMap
| React.LazyExoticComponent<React.ComponentType<P>>
loadableGenerated?: LoadableGeneratedOptions
ssr?: boolean
suspense?: boolean
}
export type LoadableOptions<P = {}> = LoadableBaseOptions<P>
export type DynamicOptions<P = {}> = LoadableBaseOptions<P>
export type LoadableFn<P = {}> = (
opts: LoadableOptions<P>
) => React.ComponentType<P>
export type LoadableComponent<P = {}> = React.ComponentType<P>
export function noSSR<P = {}>(
LoadableInitializer: LoadableFn<P>,
loadableOptions: LoadableOptions<P>
): React.ComponentType<P> {
// Removing webpack and modules means react-loadable won't try preloading
delete loadableOptions.webpack
delete loadableOptions.modules
// This check is necessary to prevent react-loadable from initializing on the server
if (!isServerSide) {
return LoadableInitializer(loadableOptions)
}
const Loading = loadableOptions.loading!
// This will only be rendered on the server side
return () => (
<Loading error={null} isLoading pastDelay={false} timedOut={false} />
)
}
export default function dynamic<P = {}>(
dynamicOptions: DynamicOptions<P> | Loader<P>,
options?: DynamicOptions<P>
): React.ComponentType<P> {
let loadableFn: LoadableFn<P> = Loadable
let loadableOptions: LoadableOptions<P> = {
// A loading component is not required, so we default it
loading: ({ error, isLoading, pastDelay }) => {
if (!pastDelay) return null
if (process.env.NODE_ENV === 'development') {
if (isLoading) {
return null
}
if (error) {
return (
<p>
{error.message}
<br />
{error.stack}
</p>
)
}
}
return null
},
}
// Support for direct import(), eg: dynamic(import('../hello-world'))
// Note that this is only kept for the edge case where someone is passing in a promise as first argument
// The react-loadable babel plugin will turn dynamic(import('../hello-world')) into dynamic(() => import('../hello-world'))
// To make sure we don't execute the import without rendering first
if (dynamicOptions instanceof Promise) {
loadableOptions.loader = () => dynamicOptions
// Support for having import as a function, eg: dynamic(() => import('../hello-world'))
} else if (typeof dynamicOptions === 'function') {
loadableOptions.loader = dynamicOptions
// Support for having first argument being options, eg: dynamic({loader: import('../hello-world')})
} else if (typeof dynamicOptions === 'object') {
loadableOptions = { ...loadableOptions, ...dynamicOptions }
}
// Support for passing options, eg: dynamic(import('../hello-world'), {loading: () => <p>Loading something</p>})
loadableOptions = { ...loadableOptions, ...options }
if (!process.env.__NEXT_REACT_ROOT) {
if (
process.env.NODE_ENV !== 'production' &&
loadableOptions.suspense &&
!isServerSide
) {
console.warn(
`Enable experimental.reactRoot or use React version above 18 to use suspense option`
)
}
loadableOptions.suspense = false
}
const { suspense, loader } = loadableOptions
// If suspense is enabled, delegate rendering to suspense
if (suspense) {
delete loadableOptions.loadableGenerated
delete loadableOptions.loading
delete loadableOptions.ssr
}
if (typeof loadableOptions.loader === 'function' && suspense) {
loadableOptions.loader = React.lazy(
loader as () => Promise<{
default: React.ComponentType<P>
}>
)
}
// coming from build/babel/plugins/react-loadable-plugin.js
if (loadableOptions.loadableGenerated) {
loadableOptions = {
...loadableOptions,
...loadableOptions.loadableGenerated,
}
}
// support for disabling server side rendering, eg: dynamic(import('../hello-world'), {ssr: false})
if (loadableOptions.ssr === false && !suspense) {
return noSSR(loadableFn, loadableOptions)
}
return loadableFn(loadableOptions)
}