From ad37a82e4e282a218a2e4abd89f9e9dea738cadb Mon Sep 17 00:00:00 2001 From: handtrix Date: Wed, 5 Feb 2020 14:41:44 +0100 Subject: [PATCH] Propertly call App.getInitialProps if used in NextAppContext --- examples/with-apollo/lib/apollo.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/examples/with-apollo/lib/apollo.js b/examples/with-apollo/lib/apollo.js index 61e48b0d4b0b2..766dd8ee89497 100644 --- a/examples/with-apollo/lib/apollo.js +++ b/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' @@ -39,6 +40,10 @@ export function withApollo(PageComponent, { ssr = true } = {}) { const { AppTree } = ctx const inAppContext = Boolean(ctx.ctx) + if (ctx.apolloClient) { + throw new Error('Multiple instances of withApollo found.') + } + // Initialize ApolloClient const apolloClient = initApolloClient() @@ -54,6 +59,8 @@ export function withApollo(PageComponent, { ssr = true } = {}) { let pageProps = {} if (PageComponent.getInitialProps) { pageProps = await PageComponent.getInitialProps(ctx) + } else if (inAppContext) { + pageProps = await App.getInitialProps(ctx) } // Only on the server: