Commit 80a6160c authored by Henrik Wenz's avatar Henrik Wenz Committed by Tim Neutkens

Update with-dynamic-import example (#5201)

Follow up on: https://github.com/zeit/next.js/pull/4639
parent 60b1d26f
module.exports = { module.exports = {
exportPathMap: function () { exportPathMap: function () {
return { return {
'/': { page: '/' }, '/': { page: '/', query: { showMore: false } },
'/about': { page: '/about' } '/about': { page: '/about' }
} }
} }
......
...@@ -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 />
......
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