From f1cb1012f20faa675d595cbdaa3c6167ef2b614c Mon Sep 17 00:00:00 2001 From: fantasticit Date: Sun, 22 May 2022 22:30:15 +0800 Subject: [PATCH] client: fix getInitialProps --- packages/client/src/pages/_app.tsx | 106 ++++++++++++++++------------- 1 file changed, 57 insertions(+), 49 deletions(-) diff --git a/packages/client/src/pages/_app.tsx b/packages/client/src/pages/_app.tsx index 2e5c1f4..64fde0b 100644 --- a/packages/client/src/pages/_app.tsx +++ b/packages/client/src/pages/_app.tsx @@ -6,60 +6,68 @@ import 'tiptap/core/styles/index.scss'; import { isMobile } from 'helpers/env'; import { IsOnMobile } from 'hooks/use-on-mobile'; import { Theme } from 'hooks/use-theme'; -import type { AppProps } from 'next/app'; +import App from 'next/app'; import Head from 'next/head'; import React from 'react'; -type P = AppProps<{ isMobile?: boolean }>; +class MyApp extends App<{ isMobile: boolean }, unknown> { + state = { + locale: '', + user: null, + }; -function MyApp(props: AppProps & { isMobile?: boolean }) { - const { Component, pageProps, isMobile } = props; + static getInitialProps = async ({ Component, ctx }) => { + const request = ctx?.req; + const getPagePropsPromise = Component.getInitialProps ? Component.getInitialProps(ctx) : Promise.resolve({}); + const [pageProps] = await Promise.all([getPagePropsPromise]); - return ( - <> - - - - - - - - - - - - - - - - - - - - - - {((process.env.DNS_PREFETCH || []) as string[]).map((url) => ( - - ))} - - - - - - - - ); + return { + pageProps, + isMobile: isMobile(request?.headers['user-agent']), + }; + }; + + render() { + const { Component, pageProps, isMobile } = this.props; + + return ( + <> + + + + + + + + + + + + + + + + + + + + + + {((process.env.DNS_PREFETCH || []) as string[]).map((url) => ( + + ))} + + + + + + + + ); + } } -MyApp.getInitialProps = async (appContext) => { - const request = appContext?.ctx?.req; - - return { - isMobile: isMobile(request?.headers['user-agent']), - }; -}; - export default MyApp;