2017-04-17 20:15:50 +00:00
|
|
|
import React from 'react'
|
2017-06-19 13:03:02 +00:00
|
|
|
import { getDisplayName } from './utils'
|
2017-04-17 20:15:50 +00:00
|
|
|
|
2017-06-16 13:19:34 +00:00
|
|
|
let currentChunks = new Set()
|
|
|
|
|
|
|
|
export default function dynamicComponent (p, o) {
|
|
|
|
let promise
|
|
|
|
let options
|
|
|
|
|
|
|
|
if (p instanceof SameLoopPromise) {
|
|
|
|
promise = p
|
|
|
|
options = o || {}
|
|
|
|
} else {
|
|
|
|
// Now we are trying to use the modules and render fields in options to load modules.
|
|
|
|
if (!p.modules || !p.render) {
|
2017-08-17 16:11:52 +00:00
|
|
|
const errorMessage = '`next/dynamic` options should contain `modules` and `render` fields'
|
2017-06-16 13:19:34 +00:00
|
|
|
throw new Error(errorMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (o) {
|
2017-08-17 16:11:52 +00:00
|
|
|
const errorMessage = 'Add additional `next/dynamic` options to the first argument containing the `modules` and `render` fields'
|
2017-06-16 13:19:34 +00:00
|
|
|
throw new Error(errorMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
options = p
|
|
|
|
}
|
2017-04-17 20:15:50 +00:00
|
|
|
|
2017-04-27 16:05:18 +00:00
|
|
|
return class DynamicComponent extends React.Component {
|
2017-04-17 20:15:50 +00:00
|
|
|
constructor (...args) {
|
|
|
|
super(...args)
|
2017-04-18 16:51:31 +00:00
|
|
|
|
2017-04-18 16:37:57 +00:00
|
|
|
this.LoadingComponent = options.loading ? options.loading : () => (<p>loading...</p>)
|
2017-04-18 16:51:31 +00:00
|
|
|
this.ssr = options.ssr === false ? options.ssr : true
|
|
|
|
|
2017-06-16 13:19:34 +00:00
|
|
|
this.state = { AsyncComponent: null, asyncElement: null }
|
2017-04-17 20:15:50 +00:00
|
|
|
this.isServer = typeof window === 'undefined'
|
2017-04-18 16:51:31 +00:00
|
|
|
|
2017-06-16 13:19:34 +00:00
|
|
|
// This flag is used to load the bundle again, if needed
|
|
|
|
this.loadBundleAgain = null
|
|
|
|
// This flag keeps track of the whether we are loading a bundle or not.
|
|
|
|
this.loadingBundle = false
|
|
|
|
|
2017-04-18 16:51:31 +00:00
|
|
|
if (this.ssr) {
|
2017-06-16 13:19:34 +00:00
|
|
|
this.load()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
load () {
|
|
|
|
if (promise) {
|
2017-04-18 16:51:31 +00:00
|
|
|
this.loadComponent()
|
2017-06-16 13:19:34 +00:00
|
|
|
} else {
|
|
|
|
this.loadBundle(this.props)
|
2017-04-18 16:51:31 +00:00
|
|
|
}
|
2017-04-17 20:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
loadComponent () {
|
2017-07-20 17:51:04 +00:00
|
|
|
promise.then((m) => {
|
|
|
|
const AsyncComponent = m.default || m
|
2017-04-27 16:05:18 +00:00
|
|
|
// Set a readable displayName for the wrapper component
|
2017-06-19 13:03:02 +00:00
|
|
|
const asyncCompName = getDisplayName(AsyncComponent)
|
2017-04-27 20:07:01 +00:00
|
|
|
if (asyncCompName) {
|
|
|
|
DynamicComponent.displayName = `DynamicComponent for ${asyncCompName}`
|
2017-04-27 16:05:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-17 20:15:50 +00:00
|
|
|
if (this.mounted) {
|
|
|
|
this.setState({ AsyncComponent })
|
|
|
|
} else {
|
|
|
|
if (this.isServer) {
|
2017-07-20 17:51:04 +00:00
|
|
|
registerChunk(m.__webpackChunkName)
|
2017-04-17 20:15:50 +00:00
|
|
|
}
|
|
|
|
this.state.AsyncComponent = AsyncComponent
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:19:34 +00:00
|
|
|
loadBundle (props) {
|
|
|
|
this.loadBundleAgain = null
|
|
|
|
this.loadingBundle = true
|
|
|
|
|
|
|
|
// Run this for prop changes as well.
|
|
|
|
const modulePromiseMap = options.modules(props)
|
|
|
|
const moduleNames = Object.keys(modulePromiseMap)
|
|
|
|
let remainingPromises = moduleNames.length
|
|
|
|
const moduleMap = {}
|
|
|
|
|
|
|
|
const renderModules = () => {
|
|
|
|
if (this.loadBundleAgain) {
|
|
|
|
this.loadBundle(this.loadBundleAgain)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loadingBundle = false
|
|
|
|
DynamicComponent.displayName = 'DynamicBundle'
|
|
|
|
const asyncElement = options.render(props, moduleMap)
|
|
|
|
if (this.mounted) {
|
|
|
|
this.setState({ asyncElement })
|
|
|
|
} else {
|
|
|
|
this.state.asyncElement = asyncElement
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const loadModule = (name) => {
|
|
|
|
const promise = modulePromiseMap[name]
|
2017-07-20 17:51:04 +00:00
|
|
|
promise.then((m) => {
|
|
|
|
const Component = m.default || m
|
2017-06-16 13:19:34 +00:00
|
|
|
if (this.isServer) {
|
2017-07-20 17:51:04 +00:00
|
|
|
registerChunk(m.__webpackChunkName)
|
2017-06-16 13:19:34 +00:00
|
|
|
}
|
|
|
|
moduleMap[name] = Component
|
|
|
|
remainingPromises--
|
|
|
|
if (remainingPromises === 0) {
|
|
|
|
renderModules()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
moduleNames.forEach(loadModule)
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:15:50 +00:00
|
|
|
componentDidMount () {
|
|
|
|
this.mounted = true
|
2017-04-18 16:51:31 +00:00
|
|
|
if (!this.ssr) {
|
2017-06-16 13:19:34 +00:00
|
|
|
this.load()
|
2017-04-18 16:51:31 +00:00
|
|
|
}
|
2017-04-17 20:15:50 +00:00
|
|
|
}
|
|
|
|
|
2017-06-16 13:19:34 +00:00
|
|
|
componentWillReceiveProps (nextProps) {
|
|
|
|
if (promise) return
|
|
|
|
|
|
|
|
this.setState({ asyncElement: null })
|
|
|
|
|
|
|
|
if (this.loadingBundle) {
|
|
|
|
this.loadBundleAgain = nextProps
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
this.loadBundle(nextProps)
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:15:50 +00:00
|
|
|
render () {
|
2017-06-16 13:19:34 +00:00
|
|
|
const { AsyncComponent, asyncElement } = this.state
|
2017-04-18 16:37:57 +00:00
|
|
|
const { LoadingComponent } = this
|
2017-04-17 20:15:50 +00:00
|
|
|
|
2017-06-16 13:19:34 +00:00
|
|
|
if (asyncElement) return asyncElement
|
|
|
|
if (AsyncComponent) return (<AsyncComponent {...this.props} />)
|
|
|
|
|
|
|
|
return (<LoadingComponent {...this.props} />)
|
2017-04-17 20:15:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 13:19:34 +00:00
|
|
|
export function registerChunk (chunk) {
|
|
|
|
currentChunks.add(chunk)
|
|
|
|
}
|
|
|
|
|
2017-04-17 20:15:50 +00:00
|
|
|
export function flushChunks () {
|
2017-06-16 13:19:34 +00:00
|
|
|
const chunks = Array.from(currentChunks)
|
|
|
|
currentChunks.clear()
|
2017-04-17 20:15:50 +00:00
|
|
|
return chunks
|
|
|
|
}
|
2017-04-19 18:25:06 +00:00
|
|
|
|
|
|
|
export class SameLoopPromise {
|
|
|
|
constructor (cb) {
|
|
|
|
this.onResultCallbacks = []
|
|
|
|
this.onErrorCallbacks = []
|
2017-04-28 00:12:20 +00:00
|
|
|
this.cb = cb
|
2017-04-19 18:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setResult (result) {
|
|
|
|
this.gotResult = true
|
|
|
|
this.result = result
|
|
|
|
this.onResultCallbacks.forEach((cb) => cb(result))
|
|
|
|
this.onResultCallbacks = []
|
|
|
|
}
|
|
|
|
|
|
|
|
setError (error) {
|
|
|
|
this.gotError = true
|
|
|
|
this.error = error
|
|
|
|
this.onErrorCallbacks.forEach((cb) => cb(error))
|
|
|
|
this.onErrorCallbacks = []
|
|
|
|
}
|
|
|
|
|
|
|
|
then (onResult, onError) {
|
2017-04-28 00:12:20 +00:00
|
|
|
this.runIfNeeded()
|
2017-04-19 18:25:06 +00:00
|
|
|
const promise = new SameLoopPromise()
|
|
|
|
|
|
|
|
const handleError = () => {
|
|
|
|
if (onError) {
|
|
|
|
promise.setResult(onError(this.error))
|
|
|
|
} else {
|
|
|
|
promise.setError(this.error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleResult = () => {
|
|
|
|
promise.setResult(onResult(this.result))
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.gotResult) {
|
|
|
|
handleResult()
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.gotError) {
|
|
|
|
handleError()
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onResultCallbacks.push(handleResult)
|
|
|
|
this.onErrorCallbacks.push(handleError)
|
|
|
|
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (onError) {
|
2017-04-28 00:12:20 +00:00
|
|
|
this.runIfNeeded()
|
2017-04-19 18:25:06 +00:00
|
|
|
const promise = new SameLoopPromise()
|
|
|
|
|
|
|
|
const handleError = () => {
|
|
|
|
promise.setResult(onError(this.error))
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleResult = () => {
|
|
|
|
promise.setResult(this.result)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.gotResult) {
|
|
|
|
handleResult()
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.gotError) {
|
|
|
|
handleError()
|
|
|
|
return promise
|
|
|
|
}
|
|
|
|
|
|
|
|
this.onErrorCallbacks.push(handleError)
|
|
|
|
this.onResultCallbacks.push(handleResult)
|
|
|
|
|
|
|
|
return promise
|
|
|
|
}
|
2017-04-28 00:12:20 +00:00
|
|
|
|
|
|
|
runIfNeeded () {
|
|
|
|
if (!this.cb) return
|
|
|
|
if (this.ran) return
|
|
|
|
|
|
|
|
this.ran = true
|
|
|
|
this.cb(
|
|
|
|
(result) => this.setResult(result),
|
|
|
|
(error) => this.setError(error)
|
|
|
|
)
|
|
|
|
}
|
2017-04-19 18:25:06 +00:00
|
|
|
}
|