Unverified Commit 20d88231 authored by Henrik Wenz's avatar Henrik Wenz Committed by GitHub

Make withApollo work with _app.js components (#8801)

* Make withApollo work with _app.js components

* Support wrapping functional _App

* Add apolloClient to NextPageContext & NextPageContext

* Propertly call App.getInitialProps if used in NextAppContext

* Add Automatic Static Optimization warning
parent 9dc0afd9
import React from 'react' import React from 'react'
import App from 'next/app'
import Head from 'next/head' import Head from 'next/head'
import { ApolloProvider } from '@apollo/react-hooks' import { ApolloProvider } from '@apollo/react-hooks'
import { ApolloClient } from 'apollo-client' import { ApolloClient } from 'apollo-client'
...@@ -28,25 +29,44 @@ export const withApollo = ({ ssr = true } = {}) => PageComponent => { ...@@ -28,25 +29,44 @@ export const withApollo = ({ ssr = true } = {}) => PageComponent => {
const displayName = const displayName =
PageComponent.displayName || PageComponent.name || 'Component' PageComponent.displayName || PageComponent.name || 'Component'
if (displayName === 'App') {
console.warn('This withApollo HOC only works with PageComponents.')
}
WithApollo.displayName = `withApollo(${displayName})` WithApollo.displayName = `withApollo(${displayName})`
} }
if (ssr || PageComponent.getInitialProps) { if (ssr || PageComponent.getInitialProps) {
WithApollo.getInitialProps = async ctx => { WithApollo.getInitialProps = async ctx => {
const { AppTree } = ctx const { AppTree } = ctx
const inAppContext = Boolean(ctx.ctx)
if (process.env.NODE_ENV === 'development') {
if (inAppContext) {
console.warn(
'Warning: You have opted-out of Automatic Static Optimization due to `withApollo` in `pages/_app`.\n' +
'Read more: https://err.sh/next.js/opt-out-auto-static-optimization\n'
)
}
}
// Initialize ApolloClient, add it to the ctx object so if (ctx.apolloClient) {
// we can use it in `PageComponent.getInitialProp`. throw new Error('Multiple instances of withApollo found.')
const apolloClient = (ctx.apolloClient = initApolloClient()) }
// Initialize ApolloClient
const apolloClient = initApolloClient()
// Add apolloClient to NextPageContext & NextAppContext
// This allows us to consume the apolloClient inside our
// custom `getInitialProps({ apolloClient })`.
ctx.apolloClient = apolloClient
if (inAppContext) {
ctx.ctx.apolloClient = apolloClient
}
// Run wrapped getInitialProps methods // Run wrapped getInitialProps methods
let pageProps = {} let pageProps = {}
if (PageComponent.getInitialProps) { if (PageComponent.getInitialProps) {
pageProps = await PageComponent.getInitialProps(ctx) pageProps = await PageComponent.getInitialProps(ctx)
} else if (inAppContext) {
pageProps = await App.getInitialProps(ctx)
} }
// Only on the server: // Only on the server:
...@@ -62,14 +82,19 @@ export const withApollo = ({ ssr = true } = {}) => PageComponent => { ...@@ -62,14 +82,19 @@ export const withApollo = ({ ssr = true } = {}) => PageComponent => {
try { try {
// Run all GraphQL queries // Run all GraphQL queries
const { getDataFromTree } = await import('@apollo/react-ssr') const { getDataFromTree } = await import('@apollo/react-ssr')
await getDataFromTree(
<AppTree // Since AppComponents and PageComponents have different context types
pageProps={{ // we need to modify their props a little.
...pageProps, let props
apolloClient, if (inAppContext) {
}} props = { ...pageProps, apolloClient }
/> } else {
) props = { pageProps: { ...pageProps, apolloClient } }
}
// Takes React AppTree, determine which queries are needed to render,
// then fetche them all.
await getDataFromTree(<AppTree {...props} />)
} catch (error) { } catch (error) {
// Prevent Apollo Client GraphQL errors from crashing SSR. // Prevent Apollo Client GraphQL errors from crashing SSR.
// Handle them in components via the data.error prop: // Handle them in components via the data.error prop:
......
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