Unverified Commit 98df70e2 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Refactor next dynamic (#44832)

* Convert `loadable` to tsx for convenience
* Merge `NoSSR` into `loadable`
* Address the missing `preload` method mentioned in
https://github.com/vercel/next.js/pull/42589#issuecomment-1353226954Co-authored-by: 's avatarkodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
parent 6ef7f5ec
...@@ -11,7 +11,7 @@ export function suspense() { ...@@ -11,7 +11,7 @@ export function suspense() {
type Child = React.ReactElement<any, any> type Child = React.ReactElement<any, any>
export default function NoSSR({ children }: { children: Child }): Child { export function NoSSR({ children }: { children: Child }): Child {
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
suspense() suspense()
} }
......
import React, { lazy, Suspense } from 'react' import React from 'react'
import Loadable from './loadable' import Loadable from './loadable'
import NoSSR from './dynamic-no-ssr'
type ComponentModule<P = {}> = { default: React.ComponentType<P> } type ComponentModule<P = {}> = { default: React.ComponentType<P> }
...@@ -8,10 +7,6 @@ export declare type LoaderComponent<P = {}> = Promise< ...@@ -8,10 +7,6 @@ export declare type LoaderComponent<P = {}> = Promise<
React.ComponentType<P> | ComponentModule<P> React.ComponentType<P> | ComponentModule<P>
> >
type NormalizedLoader<P = {}> = () => Promise<{
default: React.ComponentType<P>
}>
export declare type Loader<P = {}> = export declare type Loader<P = {}> =
| (() => LoaderComponent<P>) | (() => LoaderComponent<P>)
| LoaderComponent<P> | LoaderComponent<P>
...@@ -57,30 +52,6 @@ export type LoadableFn<P = {}> = ( ...@@ -57,30 +52,6 @@ export type LoadableFn<P = {}> = (
export type LoadableComponent<P = {}> = React.ComponentType<P> export type LoadableComponent<P = {}> = React.ComponentType<P>
export function noSSR<P = {}>(
LoadableInitializer: NormalizedLoader<P>,
loadableOptions: DynamicOptions<P>
): React.ComponentType<P> {
// 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 = (
<Loading error={null} isLoading pastDelay={false} timedOut={false} />
)
return (props: any) => (
<Suspense fallback={fallback}>
<NoSSR>
<NoSSRComponent {...props} />
</NoSSR>
</Suspense>
)
}
export default function dynamic<P = {}>( export default function dynamic<P = {}>(
dynamicOptions: DynamicOptions<P> | Loader<P>, dynamicOptions: DynamicOptions<P> | Loader<P>,
options?: DynamicOptions<P> options?: DynamicOptions<P>
...@@ -127,26 +98,25 @@ export default function dynamic<P = {}>( ...@@ -127,26 +98,25 @@ export default function dynamic<P = {}>(
loadableOptions = { ...loadableOptions, ...options } loadableOptions = { ...loadableOptions, ...options }
const loaderFn = loadableOptions.loader as () => LoaderComponent<P> const loaderFn = loadableOptions.loader as () => LoaderComponent<P>
const loader = () => loaderFn().then(convertModule) const loader = () =>
loaderFn != null
? loaderFn().then(convertModule)
: Promise.resolve(convertModule(() => null))
// coming from build/babel/plugins/react-loadable-plugin.js // coming from build/babel/plugins/react-loadable-plugin.js
if (loadableOptions.loadableGenerated) { if (loadableOptions.loadableGenerated) {
loadableOptions = { loadableOptions = {
...loadableOptions, ...loadableOptions,
...loadableOptions.loadableGenerated, ...loadableOptions.loadableGenerated,
loader,
} }
delete loadableOptions.loadableGenerated delete loadableOptions.loadableGenerated
} }
// support for disabling server side rendering, eg: dynamic(() => import('../hello-world'), {ssr: false}). // support for disabling server side rendering, eg: dynamic(() => import('../hello-world'), {ssr: false}).
if (typeof loadableOptions.ssr === 'boolean') { if (typeof loadableOptions.ssr === 'boolean' && !loadableOptions.ssr) {
if (!loadableOptions.ssr) { delete loadableOptions.webpack
delete loadableOptions.ssr delete loadableOptions.modules
return noSSR(loader, loadableOptions)
}
delete loadableOptions.ssr
} }
return loadableFn(loadableOptions) return loadableFn({ ...loadableOptions, loader: loader as Loader<P> })
} }
/* tslint:disable */
import React from 'react'
declare namespace LoadableExport {
interface ILoadable {
<P = {}>(opts: any): React.ComponentClass<P>
Map<P = {}>(opts: any): React.ComponentType<P>
preloadAll(): Promise<any>
}
}
// eslint-disable-next-line @typescript-eslint/no-redeclare
declare const LoadableExport: LoadableExport.ILoadable
export = LoadableExport
...@@ -23,6 +23,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE ...@@ -23,6 +23,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
// Modified to be compatible with webpack 4 / Next.js // Modified to be compatible with webpack 4 / Next.js
import React from 'react' import React from 'react'
import { NoSSR } from './dynamic-no-ssr'
import { LoadableContext } from './loadable-context' import { LoadableContext } from './loadable-context'
const ALL_INITIALIZERS: any[] = [] const ALL_INITIALIZERS: any[] = []
...@@ -62,6 +63,7 @@ function createLoadableComponent(loadFn: any, options: any) { ...@@ -62,6 +63,7 @@ function createLoadableComponent(loadFn: any, options: any) {
timeout: null, timeout: null,
webpack: null, webpack: null,
modules: null, modules: null,
ssr: true,
}, },
options options
) )
...@@ -117,21 +119,24 @@ function createLoadableComponent(loadFn: any, options: any) { ...@@ -117,21 +119,24 @@ function createLoadableComponent(loadFn: any, options: any) {
}) })
} }
} }
function LoadableComponent(props: any) { function LoadableComponent(props: any) {
useLoadableModule() useLoadableModule()
const fallbackElement = React.createElement(opts.loading, { const Loading = opts.loading
isLoading: true, const fallbackElement = (
pastDelay: true, <Loading isLoading={true} pastDelay={true} error={null} />
error: null, )
})
return React.createElement( const Wrap = opts.ssr ? React.Fragment : NoSSR
React.Suspense, const Lazy = opts.lazy
{
fallback: fallbackElement, return (
}, <React.Suspense fallback={fallbackElement}>
React.createElement(opts.lazy, props) <Wrap>
<Lazy {...props} />
</Wrap>
</React.Suspense>
) )
} }
......
...@@ -8,6 +8,10 @@ import dynamic from 'next/dynamic' ...@@ -8,6 +8,10 @@ import dynamic from 'next/dynamic'
describe('next/dynamic', () => { describe('next/dynamic', () => {
it('test dynamic with jest', () => { it('test dynamic with jest', () => {
const App = dynamic(() => import('./fixtures/stub-components/hello')) const App = dynamic(() => import('./fixtures/stub-components/hello'))
// @ts-ignore
expect(App.preload).toBeDefined()
act(() => { act(() => {
const { unmount } = render(<App />) const { unmount } = render(<App />)
unmount() unmount()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment