1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00

[with-apollo] Use getDataFromTree in browser (#3457)

* use getDataFromTree in browser

* formatting

* removed ssrMode argument, not needed
This commit is contained in:
Jon Espen Kvisler 2018-02-04 12:36:35 +01:00 committed by Tim Neutkens
parent 97cf4281dc
commit 4d9cf1940c

View file

@ -34,37 +34,37 @@ export default ComposedComponent => {
// Run all GraphQL queries in the component tree // Run all GraphQL queries in the component tree
// and extract the resulting data // and extract the resulting data
if (!process.browser) { const apollo = initApollo()
const apollo = initApollo() try {
// Run all GraphQL queries
try { await getDataFromTree(
// Run all GraphQL queries <ApolloProvider client={apollo}>
await getDataFromTree( <ComposedComponent {...composedInitialProps} />
<ApolloProvider client={apollo}> </ApolloProvider>,
<ComposedComponent {...composedInitialProps} /> {
</ApolloProvider>, router: {
{ asPath: ctx.asPath,
router: { pathname: ctx.pathname,
asPath: ctx.asPath, query: ctx.query
pathname: ctx.pathname,
query: ctx.query
}
} }
) }
} catch (error) { )
// Prevent Apollo Client GraphQL errors from crashing SSR. } catch (error) {
// Handle them in components via the data.error prop: // Prevent Apollo Client GraphQL errors from crashing SSR.
// http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error // Handle them in components via the data.error prop:
} // http://dev.apollodata.com/react/api-queries.html#graphql-query-data-error
}
if (!process.browser) {
// getDataFromTree does not call componentWillUnmount // getDataFromTree does not call componentWillUnmount
// head side effect therefore need to be cleared manually // head side effect therefore need to be cleared manually
Head.rewind() Head.rewind()
}
// Extract query data from the Apollo store // Extract query data from the Apollo store
serverState = { serverState = {
apollo: { apollo: {
data: apollo.cache.extract() data: apollo.cache.extract()
}
} }
} }