From 55cee10b6e6d14d8927cb9449298ac38ddb31185 Mon Sep 17 00:00:00 2001 From: Jayden Seric Date: Mon, 2 Dec 2019 14:59:18 +1100 Subject: [PATCH] Functional custom App. --- app/pages/_app.js | 97 ++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 48 deletions(-) diff --git a/app/pages/_app.js b/app/pages/_app.js index 58c1549..4346ef6 100644 --- a/app/pages/_app.js +++ b/app/pages/_app.js @@ -4,7 +4,6 @@ import { InMemoryCache } from 'apollo-cache-inmemory' import { ApolloClient } from 'apollo-client' import { createUploadLink } from 'apollo-upload-client' import { stylesGlobal, stylesGlobalTheme } from 'device-agnostic-ui' -import App from 'next/app' import Head from 'next/head' const createApolloClient = (cache = {}) => @@ -14,57 +13,59 @@ const createApolloClient = (cache = {}) => link: createUploadLink({ uri: process.env.API_URI }) }) -export default class CustomApp extends App { - static async getInitialProps({ ctx, router, Component }) { - const props = {} +const App = ({ + Component, + pageProps, + apolloCache, + apolloClient = createApolloClient(apolloCache) +}) => ( + + + + + + + + + + + +) - if (Component.getInitialProps) - props.pageProps = await Component.getInitialProps(ctx) +App.getInitialProps = async context => { + const props = { + pageProps: context.Component.getInitialProps + ? await context.Component.getInitialProps(context) + : {} + } - if (ctx.req) { - const apolloClient = createApolloClient() - try { - const { getDataFromTree } = await import('@apollo/react-ssr') - await getDataFromTree( - - ) - } catch (error) { - // Prevent crash from GraphQL errors. - console.error(error) - } - Head.rewind() - props.apolloCache = apolloClient.cache.extract() + if (context.ctx.req) { + const apolloClient = createApolloClient() + try { + const { getDataFromTree } = await import('@apollo/react-ssr') + await getDataFromTree( + + ) + } catch (error) { + // Prevent crash from GraphQL errors. + console.error(error) } - return props + Head.rewind() + + props.apolloCache = apolloClient.cache.extract() } - apolloClient = - this.props.apolloClient || createApolloClient(this.props.apolloCache) - - render() { - const { Component, pageProps = {} } = this.props - return ( - - - - - - - - - - - - ) - } + return props } + +export default App