2017-06-30 20:18:11 +00:00
|
|
|
import React from 'react'
|
|
|
|
import cookie from 'cookie'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import { ApolloProvider, getDataFromTree } from 'react-apollo'
|
2017-11-17 07:23:52 +00:00
|
|
|
import Head from 'next/head'
|
2017-06-30 20:18:11 +00:00
|
|
|
|
2017-11-02 16:56:14 +00:00
|
|
|
import initApollo from './initApollo'
|
2017-06-30 20:18:11 +00:00
|
|
|
|
2017-11-17 07:23:52 +00:00
|
|
|
function parseCookies(context = {}, options = {}) {
|
2017-06-30 20:18:11 +00:00
|
|
|
return cookie.parse(
|
2017-11-17 07:23:52 +00:00
|
|
|
context.req && context.req.headers.cookie
|
|
|
|
? context.req.headers.cookie
|
2017-06-30 20:18:11 +00:00
|
|
|
: document.cookie,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ComposedComponent => {
|
|
|
|
return class WithData extends React.Component {
|
|
|
|
static displayName = `WithData(${ComposedComponent.displayName})`
|
|
|
|
static propTypes = {
|
|
|
|
serverState: PropTypes.object.isRequired
|
|
|
|
}
|
|
|
|
|
2017-11-17 07:23:52 +00:00
|
|
|
static async getInitialProps(context) {
|
2017-06-30 20:18:11 +00:00
|
|
|
let serverState = {}
|
|
|
|
|
|
|
|
// Setup a server-side one-time-use apollo client for initial props and
|
|
|
|
// rendering (on server)
|
|
|
|
let apollo = initApollo({}, {
|
|
|
|
getToken: () => parseCookies(context).token
|
|
|
|
})
|
|
|
|
|
|
|
|
// Evaluate the composed component's getInitialProps()
|
|
|
|
let composedInitialProps = {}
|
|
|
|
if (ComposedComponent.getInitialProps) {
|
|
|
|
composedInitialProps = await ComposedComponent.getInitialProps(context, apollo)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run all graphql queries in the component tree
|
|
|
|
// and extract the resulting data
|
|
|
|
if (!process.browser) {
|
|
|
|
if (context.res && context.res.finished) {
|
|
|
|
// When redirecting, the response is finished.
|
|
|
|
// No point in continuing to render
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provide the `url` prop data in case a graphql query uses it
|
2017-11-17 07:23:52 +00:00
|
|
|
const url = { query: context.query, pathname: context.pathname }
|
|
|
|
try {
|
|
|
|
// Run all GraphQL queries
|
|
|
|
const app = (
|
|
|
|
<ApolloProvider client={apollo}>
|
|
|
|
<ComposedComponent url={url} {...composedInitialProps} />
|
|
|
|
</ApolloProvider>
|
|
|
|
)
|
|
|
|
await getDataFromTree(app, {
|
|
|
|
router: {
|
|
|
|
query: context.query,
|
|
|
|
pathname: context.pathname,
|
|
|
|
asPath: context.asPath
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
// Prevent Apollo Client GraphQL errors from crashing SSR.
|
|
|
|
// Handle them in components via the data.error prop:
|
|
|
|
// http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error
|
|
|
|
}
|
|
|
|
// getDataFromTree does not call componentWillUnmount
|
|
|
|
// head side effect therefore need to be cleared manually
|
|
|
|
Head.rewind()
|
2017-06-30 20:18:11 +00:00
|
|
|
|
|
|
|
// Extract query data from the Apollo's store
|
2017-11-17 07:23:52 +00:00
|
|
|
serverState = apollo.cache.extract()
|
2017-06-30 20:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
serverState,
|
|
|
|
...composedInitialProps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 07:23:52 +00:00
|
|
|
constructor(props) {
|
2017-06-30 20:18:11 +00:00
|
|
|
super(props)
|
|
|
|
// Note: Apollo should never be used on the server side beyond the initial
|
|
|
|
// render within `getInitialProps()` above (since the entire prop tree
|
|
|
|
// will be initialized there), meaning the below will only ever be
|
|
|
|
// executed on the client.
|
|
|
|
this.apollo = initApollo(this.props.serverState, {
|
|
|
|
getToken: () => parseCookies().token
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-11-17 07:23:52 +00:00
|
|
|
render() {
|
2017-06-30 20:18:11 +00:00
|
|
|
return (
|
|
|
|
<ApolloProvider client={this.apollo}>
|
|
|
|
<ComposedComponent {...this.props} />
|
|
|
|
</ApolloProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2017-11-17 07:23:52 +00:00
|
|
|
}
|