client: fix getInitialProps

This commit is contained in:
fantasticit 2022-05-22 22:30:15 +08:00
parent db55765e69
commit f1cb1012f2
1 changed files with 57 additions and 49 deletions

View File

@ -6,14 +6,29 @@ import 'tiptap/core/styles/index.scss';
import { isMobile } from 'helpers/env'; import { isMobile } from 'helpers/env';
import { IsOnMobile } from 'hooks/use-on-mobile'; import { IsOnMobile } from 'hooks/use-on-mobile';
import { Theme } from 'hooks/use-theme'; import { Theme } from 'hooks/use-theme';
import type { AppProps } from 'next/app'; import App from 'next/app';
import Head from 'next/head'; import Head from 'next/head';
import React from 'react'; 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 }) { static getInitialProps = async ({ Component, ctx }) => {
const { Component, pageProps, isMobile } = props; const request = ctx?.req;
const getPagePropsPromise = Component.getInitialProps ? Component.getInitialProps(ctx) : Promise.resolve({});
const [pageProps] = await Promise.all([getPagePropsPromise]);
return {
pageProps,
isMobile: isMobile(request?.headers['user-agent']),
};
};
render() {
const { Component, pageProps, isMobile } = this.props;
return ( return (
<> <>
@ -52,14 +67,7 @@ function MyApp(props: AppProps & { isMobile?: boolean }) {
</Theme.Provider> </Theme.Provider>
</> </>
); );
}
} }
MyApp.getInitialProps = async (appContext) => {
const request = appContext?.ctx?.req;
return {
isMobile: isMobile(request?.headers['user-agent']),
};
};
export default MyApp; export default MyApp;