From 04e64df35a950e98f0a6a41bb8236a71d341a6b6 Mon Sep 17 00:00:00 2001 From: Kata Date: Mon, 18 Mar 2019 04:50:58 -0500 Subject: [PATCH] app wrapper update for ctx and errors --- ui/pages/_app.js | 87 +++++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/ui/pages/_app.js b/ui/pages/_app.js index 06dd69a..6a89e16 100644 --- a/ui/pages/_app.js +++ b/ui/pages/_app.js @@ -1,73 +1,78 @@ +// @flow import * as React from 'react' import App, { Container } from 'next/app' import Head from 'next/head' import Layout from '../components/layout' import { withCookies } from '../config/rpc' +import ErrorP from './_error' + +type NextPage = React.Component & React.StatelessFunctionalComponent & { + getInitialProps: (ctx: any, ...args: any) => any +} class RoleypolyApp extends App { - static async getInitialProps ({ Component, ctx }) { + static async getInitialProps ({ Component, ctx }: { Component: NextPage, ctx: {[x:string]: any}}) { + // Fix for next/error rendering instead of our error page. + // Who knows why this would ever happen. + if (Component.displayName === 'ErrorPage' || Component.constructor.name === 'ErrorPage') { + Component = ErrorP + } + + // console.log({ Component }) + let pageProps = {} const rpc = withCookies(ctx) + const user = await rpc.getCurrentUser() + ctx.user = user + + ctx.layout = { + noBackground: false + } + if (Component.getInitialProps) { - pageProps = await Component.getInitialProps(ctx) + pageProps = await Component.getInitialProps(ctx, rpc) } - return { pageProps, user: await rpc.getCurrentUser() } + // console.log({ pageProps }) + + return { pageProps, user, layout: ctx.layout } } - componentDidMount () { - this.loadTypekit(document) - this.waitForFOUC() - } - - loadTypekit (d) { - var config = { - kitId: 'bck0pci', - scriptTimeout: 1500, - async: true - } - const h = d.documentElement - const t = setTimeout(function () { h.className = h.className.replace(/\bwf-loading\b/g, '') + ' wf-inactive' }, config.scriptTimeout) - const tk = d.createElement('script') - const s = d.getElementsByTagName('script')[0] - let f = false - let a - h.className += ' wf-loading' - tk.src = 'https://use.typekit.net/' + config.kitId + '.js' - tk.async = true - tk.onload = tk.onreadystatechange = function () { - a = this.readyState - if (f || (a && a !== 'complete' && a !== 'loaded')) return - f = true - clearTimeout(t) - try { window.Typekit.load(config) } catch (e) {} - } - s.parentNode.insertBefore(tk, s) - } - - // wait one second, add FOUC de-protection. - waitForFOUC () { + catchFOUC () { setTimeout(() => { - document.documentElement.className += ' force-active'// + if (document.documentElement) document.documentElement.className += ' force-active' }, 1500) } render () { - const { Component, pageProps, router, user } = this.props - + const { Component, pageProps, router, user, layout } = this.props + // Fix for next/error rendering instead of our error page. + // Who knows why this would ever happen. + const ErrorCaughtComponent = (Component.displayName === 'ErrorPage' || Component.constructor.name === 'ErrorPage') ? ErrorP : Component return ( - + Roleypoly + - - + + )