2017-01-22 12:27:06 +00:00
|
|
|
import { ApolloProvider, getDataFromTree } from 'react-apollo'
|
|
|
|
import React from 'react'
|
|
|
|
import 'isomorphic-fetch'
|
|
|
|
import { initClient } from './initClient'
|
|
|
|
import { initStore } from './initStore'
|
|
|
|
|
|
|
|
export default (Component) => (
|
|
|
|
class extends React.Component {
|
|
|
|
static async getInitialProps (ctx) {
|
|
|
|
const headers = ctx.req ? ctx.req.headers : {}
|
|
|
|
const client = initClient(headers)
|
|
|
|
const store = initStore(client, client.initialState)
|
|
|
|
|
2017-01-26 01:10:21 +00:00
|
|
|
const props = {
|
|
|
|
url: { query: ctx.query, pathname: ctx.pathname },
|
|
|
|
...await (Component.getInitialProps ? Component.getInitialProps(ctx) : {})
|
|
|
|
}
|
|
|
|
|
2017-01-22 12:27:06 +00:00
|
|
|
if (!process.browser) {
|
|
|
|
const app = (
|
|
|
|
<ApolloProvider client={client} store={store}>
|
2017-01-26 01:10:21 +00:00
|
|
|
<Component {...props} />
|
2017-01-22 12:27:06 +00:00
|
|
|
</ApolloProvider>
|
|
|
|
)
|
|
|
|
await getDataFromTree(app)
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = store.getState()
|
|
|
|
return {
|
|
|
|
initialState: {
|
|
|
|
...state,
|
2017-02-07 11:02:28 +00:00
|
|
|
[client.reduxRootKey]: {
|
|
|
|
data: client.getInitialState().data
|
2017-01-22 12:27:06 +00:00
|
|
|
}
|
|
|
|
},
|
2017-01-26 01:10:21 +00:00
|
|
|
headers,
|
|
|
|
...props
|
2017-01-22 12:27:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor (props) {
|
|
|
|
super(props)
|
|
|
|
this.client = initClient(this.props.headers)
|
|
|
|
this.store = initStore(this.client, this.props.initialState)
|
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return (
|
|
|
|
<ApolloProvider client={this.client} store={this.store}>
|
2017-01-26 01:10:21 +00:00
|
|
|
<Component {...this.props} />
|
2017-01-22 12:27:06 +00:00
|
|
|
</ApolloProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|