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
80a6160c
Commit
80a6160c
authored
Sep 23, 2018
by
Henrik Wenz
Committed by
Tim Neutkens
Sep 23, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update with-dynamic-import example (#5201)
Follow up on:
https://github.com/zeit/next.js/pull/4639
parent
60b1d26f
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
34 deletions
+45
-34
next.config.js
examples/with-dynamic-import/next.config.js
+1
-1
index.js
examples/with-dynamic-import/pages/index.js
+44
-33
No files found.
examples/with-dynamic-import/next.config.js
View file @
80a6160c
module
.
exports
=
{
module
.
exports
=
{
exportPathMap
:
function
()
{
exportPathMap
:
function
()
{
return
{
return
{
'/'
:
{
page
:
'/'
},
'/'
:
{
page
:
'/'
,
query
:
{
showMore
:
false
}
},
'/about'
:
{
page
:
'/about'
}
'/about'
:
{
page
:
'/about'
}
}
}
}
}
...
...
examples/with-dynamic-import/pages/index.js
View file @
80a6160c
...
@@ -4,35 +4,39 @@ import Header from '../components/Header'
...
@@ -4,35 +4,39 @@ import Header from '../components/Header'
import
Counter
from
'../components/Counter'
import
Counter
from
'../components/Counter'
import
dynamic
from
'next/dynamic'
import
dynamic
from
'next/dynamic'
const
DynamicComponent
=
dynamic
(
import
(
'../components/hello1'
))
const
DynamicComponent1
=
dynamic
(
import
(
'../components/hello1'
))
const
DynamicComponentWithCustomLoading
=
dynamic
(
import
(
'../components/hello2'
),
const
DynamicComponent2WithCustomLoading
=
dynamic
({
{
loader
:
()
=>
import
(
'../components/hello2'
),
loading
:
()
=>
(
<
p
>
...
<
/p>
)
loading
:
()
=>
(
<
p
>
Loading
caused
by
client
page
transition
...
<
/p>
)
}
})
)
const
DynamicComponentWithNoSSR
=
dynamic
(
const
DynamicComponent3WithNoSSR
=
dynamic
({
import
(
'../components/hello3'
),
loader
:
()
=>
import
(
'../components/hello3'
),
{
ssr
:
false
}
loading
:
()
=>
(
<
p
>
Loading
...
<
/p>
)
,
)
ssr
:
false
const
DynamicComponent5
=
dynamic
(
import
(
'../components/hello5'
))
})
const
DynamicComponent4
=
dynamic
({
loader
:
()
=>
import
(
'../components/hello4'
)
})
const
DynamicComponent5
=
dynamic
({
loader
:
()
=>
import
(
'../components/hello5'
)
})
const
DynamicBundle
=
dynamic
({
const
DynamicBundle
=
dynamic
({
modules
:
(
props
)
=>
{
modules
:
()
=>
{
const
components
=
{
const
components
=
{
Hello6
:
import
(
'../components/hello6'
)
Hello6
:
import
(
'../components/hello6'
),
Hello7
:
import
(
'../components/hello7'
)
}
}
if
(
props
.
showMore
)
{
components
.
Hello7
=
import
(
'../components/hello7'
)
}
return
components
return
components
},
},
render
:
(
props
,
{
Hello6
,
Hello7
})
=>
(
render
:
(
props
,
{
Hello6
,
Hello7
})
=>
(
<
div
style
=
{{
padding
:
10
,
border
:
'1px solid #888'
}}
>
<
div
style
=
{{
padding
:
10
,
border
:
'1px solid #888'
}}
>
<
Hello6
/>
<
Hello6
/>
{
Hello7
?
<
Hello7
/>
:
null
}
<
Hello7
/>
<
/div
>
<
/div
>
)
)
})
})
...
@@ -42,7 +46,7 @@ export default class Index extends React.Component {
...
@@ -42,7 +46,7 @@ export default class Index extends React.Component {
return
{
showMore
:
Boolean
(
query
.
showMore
)
}
return
{
showMore
:
Boolean
(
query
.
showMore
)
}
}
}
toggleShowMore
()
{
toggleShowMore
=
()
=>
{
const
{
showMore
}
=
this
.
props
const
{
showMore
}
=
this
.
props
if
(
showMore
)
{
if
(
showMore
)
{
Router
.
push
(
'/'
)
Router
.
push
(
'/'
)
...
@@ -58,18 +62,25 @@ export default class Index extends React.Component {
...
@@ -58,18 +62,25 @@ export default class Index extends React.Component {
return
(
return
(
<
div
>
<
div
>
<
Header
/>
<
Header
/>
<
DynamicComponent
/>
<
DynamicComponentWithCustomLoading
/>
{
/* Load immediately, but in a sepeate bundle */
}
<
DynamicComponentWithNoSSR
/>
<
DynamicComponent1
/>
<
DynamicBundle
showMore
=
{
showMore
}
/
>
<
button
onClick
=
{()
=>
this
.
toggleShowMore
()}
>
Toggle
Show
More
<
/button
>
{
/* Show a progress indicator while loading */
}
{
<
DynamicComponent2WithCustomLoading
/>
/*
Since DynamicComponent5 does not render in the client,
{
/* Load only on the client side */
}
it won't get downloaded.
<
DynamicComponent3WithNoSSR
/>
*/
}
{
/* This component will never be loaded */
}
{
React
.
noSuchField
===
true
?
<
DynamicComponent5
/>
:
null
}
{
React
.
noSuchField
&&
<
DynamicComponent4
/>
}
{
/* Load on demand */
}
{
showMore
&&
<
DynamicComponent5
/>
}
<
button
onClick
=
{
this
.
toggleShowMore
}
>
Toggle
Show
More
<
/button
>
{
/* Load multible components in one bundle */
}
<
DynamicBundle
/>
<
p
>
HOME
PAGE
is
here
!<
/p
>
<
p
>
HOME
PAGE
is
here
!<
/p
>
<
Counter
/>
<
Counter
/>
...
...
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