From 4daef52d648db9953e68fa99495db047833a4ad9 Mon Sep 17 00:00:00 2001 From: Wes Bos Date: Tue, 15 May 2018 11:01:32 -0400 Subject: move user check to _app --- frontend/pages/_app.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'frontend/pages/_app.js') diff --git a/frontend/pages/_app.js b/frontend/pages/_app.js index 27202d5..b23df9b 100644 --- a/frontend/pages/_app.js +++ b/frontend/pages/_app.js @@ -1,13 +1,19 @@ -import App, { Container } from 'next/app'; -import React from 'react'; import { ApolloApp } from 'next-with-apollo'; import withData from '../lib/withData'; +import { CURRENT_USER_QUERY } from '../queries/queries'; // Next.js wraps each Page in an component. This is handy for when you want to persist anything from page to page, or just access a component that is 1 level higher than each page. -// This code is taken right from the Next.js docs, and then we add the Router to the page props, which is why we need a custom _app.js +// normall here you import App from next/app, but we use next-with-apollo's ApolloApp here so we can get SSR class MyApp extends ApolloApp { + componentDidMount() { + if (typeof window !== 'undefined' && !window.__CLIENTLOADED__) { + console.log('Refetching current user'); + this.props.apollo.query({ query: CURRENT_USER_QUERY, fetchPolicy: 'network-only' }); + window.__CLIENTLOADED__ = true; + } + } static async getInitialProps({ Component, ctx }) { let pageProps = {}; @@ -15,7 +21,7 @@ class MyApp extends ApolloApp { pageProps = await Component.getInitialProps(ctx); } - pageProps.query = ctx.query + pageProps.query = ctx.query; return { pageProps }; } -- cgit v1.3