2017-04-17 15:33:40 +00:00
|
|
|
import React from 'react'
|
2017-06-16 13:19:34 +00:00
|
|
|
import Router from 'next/router'
|
2017-04-17 15:33:40 +00:00
|
|
|
import Header from '../components/Header'
|
|
|
|
import Counter from '../components/Counter'
|
2017-04-17 20:15:50 +00:00
|
|
|
import dynamic from 'next/dynamic'
|
2017-04-17 15:33:40 +00:00
|
|
|
|
2018-09-23 14:01:54 +00:00
|
|
|
const DynamicComponent1 = dynamic(import('../components/hello1'))
|
|
|
|
|
|
|
|
const DynamicComponent2WithCustomLoading = dynamic({
|
|
|
|
loader: () => import('../components/hello2'),
|
|
|
|
loading: () => (<p>Loading caused by client page transition ...</p>)
|
|
|
|
})
|
|
|
|
|
|
|
|
const DynamicComponent3WithNoSSR = dynamic({
|
|
|
|
loader: () => import('../components/hello3'),
|
|
|
|
loading: () => (<p>Loading ...</p>),
|
|
|
|
ssr: false
|
|
|
|
})
|
|
|
|
|
|
|
|
const DynamicComponent4 = dynamic({
|
|
|
|
loader: () => import('../components/hello4')
|
|
|
|
})
|
|
|
|
|
|
|
|
const DynamicComponent5 = dynamic({
|
|
|
|
loader: () => import('../components/hello5')
|
|
|
|
})
|
2017-05-15 04:44:26 +00:00
|
|
|
|
2017-06-16 13:19:34 +00:00
|
|
|
const DynamicBundle = dynamic({
|
2018-09-23 14:01:54 +00:00
|
|
|
modules: () => {
|
2017-06-16 13:19:34 +00:00
|
|
|
const components = {
|
2018-09-23 14:01:54 +00:00
|
|
|
Hello6: import('../components/hello6'),
|
|
|
|
Hello7: import('../components/hello7')
|
2017-05-15 04:44:26 +00:00
|
|
|
}
|
2017-06-16 13:19:34 +00:00
|
|
|
return components
|
|
|
|
},
|
|
|
|
render: (props, { Hello6, Hello7 }) => (
|
|
|
|
<div style={{padding: 10, border: '1px solid #888'}}>
|
|
|
|
<Hello6 />
|
2018-09-23 14:01:54 +00:00
|
|
|
<Hello7 />
|
2017-06-16 13:19:34 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
export default class Index extends React.Component {
|
|
|
|
static getInitialProps ({ query }) {
|
|
|
|
return { showMore: Boolean(query.showMore) }
|
|
|
|
}
|
|
|
|
|
2018-09-23 14:01:54 +00:00
|
|
|
toggleShowMore = () => {
|
2017-06-16 13:19:34 +00:00
|
|
|
const { showMore } = this.props
|
|
|
|
if (showMore) {
|
|
|
|
Router.push('/')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
Router.push('/?showMore=1')
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const { showMore } = this.props
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Header />
|
2018-09-23 14:01:54 +00:00
|
|
|
|
|
|
|
{/* Load immediately, but in a sepeate bundle */}
|
|
|
|
<DynamicComponent1 />
|
|
|
|
|
|
|
|
{/* Show a progress indicator while loading */}
|
|
|
|
<DynamicComponent2WithCustomLoading />
|
|
|
|
|
|
|
|
{/* Load only on the client side */}
|
|
|
|
<DynamicComponent3WithNoSSR />
|
|
|
|
|
|
|
|
{/* This component will never be loaded */}
|
|
|
|
{React.noSuchField && <DynamicComponent4 />}
|
|
|
|
|
|
|
|
{/* Load on demand */}
|
|
|
|
{showMore && <DynamicComponent5 />}
|
|
|
|
<button onClick={this.toggleShowMore}>Toggle Show More</button>
|
|
|
|
|
|
|
|
{/* Load multible components in one bundle */}
|
|
|
|
<DynamicBundle />
|
2017-06-16 13:19:34 +00:00
|
|
|
|
|
|
|
<p>HOME PAGE is here!</p>
|
|
|
|
<Counter />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|