Commit 35ffad96 authored by Brice BERNARD's avatar Brice BERNARD Committed by Tim Neutkens

[with-apollo] Fix missing rootContext (#3468)

parent 4a3aa261
import Link from 'next/link' import Link from 'next/link'
import { withRouter } from 'next/router'
export default ({ pathname }) => ( const Header = ({ router: { pathname } }) => (
<header> <header>
<Link prefetch href='/'> <Link prefetch href='/'>
<a className={pathname === '/' ? 'is-active' : ''}>Home</a> <a className={pathname === '/' ? 'is-active' : ''}>Home</a>
...@@ -23,3 +24,5 @@ export default ({ pathname }) => ( ...@@ -23,3 +24,5 @@ export default ({ pathname }) => (
`}</style> `}</style>
</header> </header>
) )
export default withRouter(Header)
...@@ -36,14 +36,20 @@ export default ComposedComponent => { ...@@ -36,14 +36,20 @@ export default ComposedComponent => {
// and extract the resulting data // and extract the resulting data
if (!process.browser) { if (!process.browser) {
const apollo = initApollo() const apollo = initApollo()
// Provide the `url` prop data in case a GraphQL query uses it
const url = { query: ctx.query, pathname: ctx.pathname }
try { try {
// Run all GraphQL queries // Run all GraphQL queries
await getDataFromTree( await getDataFromTree(
<ApolloProvider client={apollo}> <ApolloProvider client={apollo}>
<ComposedComponent url={url} {...composedInitialProps} /> <ComposedComponent {...composedInitialProps} />
</ApolloProvider> </ApolloProvider>,
{
router: {
asPath: ctx.asPath,
pathname: ctx.pathname,
query: ctx.query
}
}
) )
} catch (error) { } catch (error) {
// Prevent Apollo Client GraphQL errors from crashing SSR. // Prevent Apollo Client GraphQL errors from crashing SSR.
......
import App from '../components/App' import App from '../components/App'
import Header from '../components/Header' import Header from '../components/Header'
export default props => ( export default () => (
<App> <App>
<Header pathname={props.url.pathname} /> <Header />
<article> <article>
<h1>The Idea Behind This Example</h1> <h1>The Idea Behind This Example</h1>
<p> <p>
......
...@@ -4,9 +4,9 @@ import Submit from '../components/Submit' ...@@ -4,9 +4,9 @@ import Submit from '../components/Submit'
import PostList from '../components/PostList' import PostList from '../components/PostList'
import withData from '../lib/withData' import withData from '../lib/withData'
export default withData(props => ( export default withData(() => (
<App> <App>
<Header pathname={props.url.pathname} /> <Header />
<Submit /> <Submit />
<PostList /> <PostList />
</App> </App>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment