1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/lib/dynamic.js
2017-04-18 22:21:31 +05:30

56 lines
1.3 KiB
JavaScript

import React from 'react'
let currentChunks = []
export default function dynamicComponent (promise, options = {}) {
return class Comp extends React.Component {
constructor (...args) {
super(...args)
this.LoadingComponent = options.loading ? options.loading : () => (<p>loading...</p>)
this.ssr = options.ssr === false ? options.ssr : true
this.state = { AsyncComponent: null }
this.isServer = typeof window === 'undefined'
if (this.ssr) {
this.loadComponent()
}
}
loadComponent () {
promise.then((AsyncComponent) => {
if (this.mounted) {
this.setState({ AsyncComponent })
} else {
if (this.isServer) {
currentChunks.push(AsyncComponent.__webpackChunkName)
}
this.state.AsyncComponent = AsyncComponent
}
})
}
componentDidMount () {
this.mounted = true
if (!this.ssr) {
this.loadComponent()
}
}
render () {
const { AsyncComponent } = this.state
const { LoadingComponent } = this
if (!AsyncComponent) return (<LoadingComponent {...this.props} />)
return <AsyncComponent {...this.props} />
}
}
}
export function flushChunks () {
const chunks = currentChunks
currentChunks = []
return chunks
}