Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
d3205561
Unverified
Commit
d3205561
authored
Dec 15, 2023
by
Jiachi Liu
Committed by
GitHub
Dec 15, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor and simplify app dynamic components (#59658)
parent
7faaeeb9
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
57 deletions
+62
-57
app-dynamic.tsx
packages/next/src/shared/lib/app-dynamic.tsx
+13
-39
loadable.tsx
packages/next/src/shared/lib/lazy-dynamic/loadable.tsx
+27
-18
types.ts
packages/next/src/shared/lib/lazy-dynamic/types.ts
+22
-0
No files found.
packages/next/src/shared/lib/app-dynamic.tsx
View file @
d3205561
import
React
from
'react'
import
Loadable
from
'./lazy-dynamic/loadable'
type
ComponentModule
<
P
=
{}
>
=
{
default
:
React
.
ComponentType
<
P
>
}
export
declare
type
LoaderComponent
<
P
=
{}
>
=
Promise
<
React
.
ComponentType
<
P
>
|
ComponentModule
<
P
>
>
export
declare
type
Loader
<
P
=
{}
>
=
()
=>
LoaderComponent
<
P
>
export
type
LoaderMap
=
{
[
module
:
string
]:
()
=>
Loader
<
any
>
}
export
type
LoadableGeneratedOptions
=
{
webpack
?():
any
modules
?():
LoaderMap
}
export
type
DynamicOptionsLoadingProps
=
{
error
?:
Error
|
null
isLoading
?:
boolean
pastDelay
?:
boolean
retry
?:
()
=>
void
timedOut
?:
boolean
}
// Normalize loader to return the module as form { default: Component } for `React.lazy`.
// Also for backward compatible since next/dynamic allows to resolve a component directly with loader
// Client component reference proxy need to be converted to a module.
function
convertModule
<
P
>
(
mod
:
React
.
ComponentType
<
P
>
|
ComponentModule
<
P
>
)
{
return
{
default
:
(
mod
as
ComponentModule
<
P
>
)?.
default
||
mod
}
import
type
{
LoadableGeneratedOptions
,
DynamicOptionsLoadingProps
,
Loader
,
LoaderComponent
,
}
from
'./lazy-dynamic/types'
export
{
type
LoadableGeneratedOptions
,
type
DynamicOptionsLoadingProps
,
type
Loader
,
type
LoaderComponent
,
}
export
type
DynamicOptions
<
P
=
{}
>
=
LoadableGeneratedOptions
&
{
...
...
@@ -50,8 +34,6 @@ export default function dynamic<P = {}>(
dynamicOptions
:
DynamicOptions
<
P
>
|
Loader
<
P
>
,
options
?:
DynamicOptions
<
P
>
):
React
.
ComponentType
<
P
>
{
const
loadableFn
:
LoadableFn
<
P
>
=
Loadable
const
loadableOptions
:
LoadableOptions
<
P
>
=
{
// A loading component is not required, so we default it
loading
:
({
error
,
isLoading
,
pastDelay
})
=>
{
...
...
@@ -78,13 +60,5 @@ export default function dynamic<P = {}>(
loadableOptions
.
loader
=
dynamicOptions
}
Object
.
assign
(
loadableOptions
,
options
)
const
loaderFn
=
loadableOptions
.
loader
as
()
=>
LoaderComponent
<
P
>
const
loader
=
()
=>
loaderFn
!=
null
?
loaderFn
().
then
(
convertModule
)
:
Promise
.
resolve
(
convertModule
(()
=>
null
))
return
loadableFn
({
...
loadableOptions
,
loader
:
loader
as
Loader
<
P
>
})
return
Loadable
({
...
loadableOptions
,
...
options
})
}
packages/next/src/shared/lib/lazy-dynamic/loadable.tsx
View file @
d3205561
import
React
from
'react'
import
{
Suspense
,
lazy
,
Fragment
}
from
'react'
import
{
NoSSR
}
from
'./dynamic-no-ssr'
import
type
{
ComponentModule
}
from
'./types'
// Normalize loader to return the module as form { default: Component } for `React.lazy`.
// Also for backward compatible since next/dynamic allows to resolve a component directly with loader
// Client component reference proxy need to be converted to a module.
function
convertModule
<
P
>
(
mod
:
React
.
ComponentType
<
P
>
|
ComponentModule
<
P
>
)
{
return
{
default
:
(
mod
as
ComponentModule
<
P
>
)?.
default
||
mod
}
}
function
Loadable
(
options
:
any
)
{
const
opts
=
Object
.
assign
(
{
loader
:
null
,
loading
:
null
,
ssr
:
true
,
},
options
)
const
opts
=
{
loader
:
null
,
loading
:
null
,
ssr
:
true
,
...
options
,
}
opts
.
lazy
=
React
.
lazy
(
opts
.
loader
)
const
loader
=
()
=>
opts
.
loader
!=
null
?
opts
.
loader
().
then
(
convertModule
)
:
Promise
.
resolve
(
convertModule
(()
=>
null
))
const
Lazy
=
lazy
(
loader
)
const
Loading
=
opts
.
loading
const
Wrap
=
opts
.
ssr
?
Fragment
:
NoSSR
function
LoadableComponent
(
props
:
any
)
{
const
Loading
=
opts
.
loading
const
fallbackElement
=
(
const
fallbackElement
=
Loading
?
(
<
Loading
isLoading=
{
true
}
pastDelay=
{
true
}
error=
{
null
}
/>
)
const
Wrap
=
opts
.
ssr
?
React
.
Fragment
:
NoSSR
const
Lazy
=
opts
.
lazy
)
:
null
return
(
<
React
.
Suspense
fallback=
{
fallbackElement
}
>
<
Suspense
fallback=
{
fallbackElement
}
>
<
Wrap
>
<
Lazy
{
...
props
}
/>
</
Wrap
>
</
React
.
Suspense
>
</
Suspense
>
)
}
...
...
packages/next/src/shared/lib/lazy-dynamic/types.ts
0 → 100644
View file @
d3205561
export
type
ComponentModule
<
P
=
{}
>
=
{
default
:
React
.
ComponentType
<
P
>
}
export
declare
type
LoaderComponent
<
P
=
{}
>
=
Promise
<
React
.
ComponentType
<
P
>
|
ComponentModule
<
P
>
>
export
declare
type
Loader
<
P
=
{}
>
=
()
=>
LoaderComponent
<
P
>
export
type
LoaderMap
=
{
[
module
:
string
]:
()
=>
Loader
<
any
>
}
export
type
LoadableGeneratedOptions
=
{
webpack
?():
any
modules
?():
LoaderMap
}
export
type
DynamicOptionsLoadingProps
=
{
error
?:
Error
|
null
isLoading
?:
boolean
pastDelay
?:
boolean
retry
?:
()
=>
void
timedOut
?:
boolean
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment