diff --git a/app/pages/_app.js b/app/pages/_app.js new file mode 100644 index 0000000..6d7429f --- /dev/null +++ b/app/pages/_app.js @@ -0,0 +1,18 @@ +import { ApolloProvider } from 'react-apollo' +import App, { Container } from 'next/app' +import withApollo from '../withApollo' + +class CustomApp extends App { + render() { + const { Component, pageProps, apolloClient } = this.props + return ( + + + + + + ) + } +} + +export default withApollo(CustomApp) diff --git a/app/pages/index.js b/app/pages/index.js index 4eef198..356f60e 100644 --- a/app/pages/index.js +++ b/app/pages/index.js @@ -4,7 +4,6 @@ import UploadFile from '../components/UploadFile' import UploadFileList from '../components/UploadFileList' import UploadBlob from '../components/UploadBlob' import Uploads from '../components/Uploads' -import withData from '../providers/with-data' const IndexPage = () => ( @@ -29,4 +28,4 @@ const IndexPage = () => ( ) -export default withData(IndexPage) +export default IndexPage diff --git a/app/providers/with-data.js b/app/providers/with-data.js deleted file mode 100644 index 5bd8203..0000000 --- a/app/providers/with-data.js +++ /dev/null @@ -1,78 +0,0 @@ -import 'cross-fetch/polyfill' -import React, { Component } from 'react' -import getDisplayName from 'react-display-name' -import { ApolloClient } from 'apollo-client' -import { InMemoryCache } from 'apollo-cache-inmemory' -import { createUploadLink } from 'apollo-upload-client' -import { getDataFromTree, ApolloProvider } from 'react-apollo' -import { Router } from 'next/router' -import App from 'next/app' - -// Shares the browser ApolloClient instance between pages. -let browserApolloClient - -const createApolloClient = (cache = {}) => - new ApolloClient({ - ssrMode: typeof window !== 'undefined', - cache: new InMemoryCache().restore(cache), - link: createUploadLink({ uri: process.env.API_URI }) - }) - -export default Composed => - class WithData extends Component { - constructor(props) { - super(props) - - // Set the ApolloClient instance used in render(). - this.apolloClient = - typeof window !== 'undefined' - ? // Client: Shared instance, created at first render after SSR. - (browserApolloClient = - browserApolloClient || createApolloClient(props.cache)) - : // Server: Private instance for SSR. - createApolloClient(props.cache) - } - - static displayName = `WithApollo(${getDisplayName(Composed)})` - - static async getInitialProps(ctx) { - // Use initial props from nested page decorators. - const props = Composed.getInitialProps - ? await Composed.getInitialProps(ctx) - : {} - - // If server environment, preload the page. - if (ctx.req) { - const apolloClient = createApolloClient() - - try { - await getDataFromTree( - - ) - } catch (error) { - // Prevent crash from GraphQL errors. - } - - props.cache = apolloClient.cache.extract() - } - - return props - } - - static renderPage = ({ apolloClient, pageProps }) => ( - - - - ) - - render() { - return this.constructor.renderPage({ - apolloClient: this.apolloClient, - pageProps: this.props - }) - } - } diff --git a/app/withApollo.js b/app/withApollo.js new file mode 100644 index 0000000..1f24c71 --- /dev/null +++ b/app/withApollo.js @@ -0,0 +1,77 @@ +import 'cross-fetch/polyfill' +import { InMemoryCache } from 'apollo-cache-inmemory' +import { ApolloClient } from 'apollo-client' +import { createUploadLink } from 'apollo-upload-client' +import Head from 'next/head' +import { Component } from 'react' +import { getDataFromTree } from 'react-apollo' +import getDisplayName from 'react-display-name' + +let apolloClient + +const createApolloClient = (cache = {}) => + new ApolloClient({ + ssrMode: typeof window !== 'undefined', + cache: new InMemoryCache().restore(cache), + link: createUploadLink({ uri: process.env.API_URI }) + }) + +export default App => + class WithApollo extends Component { + static displayName = `WithApollo(${getDisplayName(App)})` + + static async getInitialProps(context) { + const props = App.getInitialProps + ? await App.getInitialProps(context) + : {} + + // If server environment, preload the page. + if (context.ctx.req) { + const apolloClient = createApolloClient() + + try { + await getDataFromTree( + + ) + } catch (error) { + // Prevent crash from GraphQL errors. + // eslint-disable-next-line no-console + console.error(error) + } + + props.apolloCache = apolloClient.cache.extract() + + Head.rewind() + } + + return props + } + + constructor(props) { + super(props) + + // Set the ApolloClient instance used in render(). + this.apolloClient = + typeof window !== 'undefined' + ? // Client: Shared instance, created at first render after SSR. + (apolloClient = + apolloClient || createApolloClient(props.apolloCache)) + : // Server: Private instance for SSR. + createApolloClient(props.apolloCache) + } + + render() { + const { + // eslint-disable-next-line no-unused-vars + apolloCache, + ...appProps + } = this.props + + return + } + }