_app.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import App from 'next/app'
  2. import Page from '../components/page'
  3. import { ApolloProvider } from 'react-apollo'
  4. import withApollo from '../lib/withApollo'
  5. /**
  6. * Next.js uses the `App` component to initialize pages. See:
  7. * https://github.com/zeit/next.js/#custom-app
  8. *
  9. * Example how to use it to style child components:
  10. * https://github.com/zeit/next.js/blob/canary/examples/with-app-layout/pages/_app.js
  11. *
  12. * Instead of the Layout component, we use the Page component here,
  13. * where we add a styled-components theme provider, and Next.js headers and metas.
  14. * - Using Page with layout information
  15. *
  16. * Using next-with-apollo:
  17. * https://github.com/lfades/next-with-apollo
  18. * - Wrapping MyApp in withApollo HOC
  19. */
  20. class MyApp extends App {
  21. static async getInitialProps ({ Component, ctx }) {
  22. let pageProps = {}
  23. if (Component.getInitialProps) {
  24. pageProps = await Component.getInitialProps(ctx)
  25. }
  26. // Add the query object to the pageProps
  27. // https://github.com/wesbos/Advanced-React/blob/master/finished-application/frontend/pages/_app.js
  28. pageProps.query = ctx.query
  29. return { pageProps }
  30. }
  31. render () {
  32. const { Component, apollo, pageProps } = this.props
  33. return (
  34. <ApolloProvider client={apollo}>
  35. <Page id='page'>
  36. <Component {...pageProps} />
  37. </Page>
  38. </ApolloProvider>
  39. )
  40. }
  41. }
  42. export default withApollo(MyApp)