Skip to content

Commit

Permalink
simplify loadable component
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Aug 15, 2021
1 parent 5b12dd5 commit 5b85297
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions packages/next/shared/lib/loadable.js
Expand Up @@ -71,8 +71,14 @@ function createLoadableComponent(loadFn, options) {
options
)

let lazyComponent
if (opts.suspense) {
opts.lazy = React.lazy(opts.loader)
lazyComponent = React.lazy(opts.loader)
return React.forwardRef(LazyImpl)
}

function LazyImpl(props, ref) {
return React.createElement(lazyComponent, { ...props, ref })
}

let subscription = null
Expand All @@ -90,7 +96,7 @@ function createLoadableComponent(loadFn, options) {
}

// Server only
if (typeof window === 'undefined' && !opts.suspense) {
if (typeof window === 'undefined') {
ALL_INITIALIZERS.push(init)
}

Expand All @@ -99,8 +105,7 @@ function createLoadableComponent(loadFn, options) {
!initialized &&
typeof window !== 'undefined' &&
typeof opts.webpack === 'function' &&
typeof require.resolveWeak === 'function' &&
!opts.suspense
typeof require.resolveWeak === 'function'
) {
const moduleIds = opts.webpack()
READY_INITIALIZERS.push((ids) => {
Expand Down Expand Up @@ -148,12 +153,8 @@ function createLoadableComponent(loadFn, options) {
}, [props, state])
}

function LazyImpl(props, ref) {
return React.createElement(opts.lazy, { ...props, ref })
}

const LoadableComponent = opts.suspense ? LazyImpl : LoadableImpl
LoadableComponent.preload = () => !opts.suspense && init()
const LoadableComponent = LoadableImpl
LoadableComponent.preload = () => init()
LoadableComponent.displayName = 'LoadableComponent'

return React.forwardRef(LoadableComponent)
Expand Down

0 comments on commit 5b85297

Please sign in to comment.