Skip to content

Commit

Permalink
Propertly call App.getInitialProps if used in NextAppContext
Browse files Browse the repository at this point in the history
  • Loading branch information
HaNdTriX committed Feb 6, 2020
1 parent 72b9f67 commit 82a5468
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/with-apollo/lib/apollo.js
@@ -1,4 +1,5 @@
import React from 'react'
import App from 'next/app'
import Head from 'next/head'
import { ApolloProvider } from '@apollo/react-hooks'
import { ApolloClient } from 'apollo-client'
Expand Down Expand Up @@ -36,6 +37,10 @@ export const withApollo = ({ ssr = true } = {}) => PageComponent => {
const { AppTree } = ctx
const inAppContext = Boolean(ctx.ctx)

if (ctx.apolloClient) {
throw new Error('Multiple instances of withApollo found.')
}

// Initialize ApolloClient
const apolloClient = initApolloClient()

Expand All @@ -51,6 +56,8 @@ export const withApollo = ({ ssr = true } = {}) => PageComponent => {
let pageProps = {}
if (PageComponent.getInitialProps) {
pageProps = await PageComponent.getInitialProps(ctx)
} else if (inAppContext) {
pageProps = await App.getInitialProps(ctx)
}

// Only on the server:
Expand Down

0 comments on commit 82a5468

Please sign in to comment.