_app.tsx 3.3 KB
Newer Older
1
import App, {AppContext, AppProps} from 'next/app';
2
import {Router, appWithTranslation} from '~/utils/i18n';
3
import {fetcher, getApiToken, setApiToken} from '~/utils/fetch';
4

5
import {GlobalStyle} from '~/utils/style';
6
import Head from 'next/head';
7
import Layout from '~/components/Layout';
8
import NProgress from 'nprogress';
9
import React from 'react';
10
import {SWRConfig} from 'swr';
11 12 13
import {ToastContainer} from 'react-toastify';
import queryString from 'query-string';
import {withRouter} from 'next/router';
14

15
class VDLApp extends App {
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    constructor(props: AppProps) {
        super(props);
        if (process.browser && process.env.API_TOKEN_KEY) {
            const query = queryString.parse(window.location.search);
            setApiToken(query[process.env.API_TOKEN_KEY]);
        }
    }

    componentDidMount() {
        Router.events.on('routeChangeStart', () => NProgress.start());
        Router.events.on('routeChangeComplete', (url: string) => {
            NProgress.done();
            if (process.env.API_TOKEN_KEY) {
                const id = getApiToken();
                const parsed = queryString.parseUrl(url);
                if (id && !parsed.query[process.env.API_TOKEN_KEY]) {
                    this.props.router.replace(
                        queryString.stringifyUrl({
                            url: parsed.url,
                            query: {
                                ...parsed.query,
                                [process.env.API_TOKEN_KEY]: id
                            }
                        }),
                        undefined,
                        {shallow: true}
                    );
                }
            }
        });
        Router.events.on('routeChangeError', () => NProgress.done());
    }

49 50 51 52 53 54 55
    render() {
        const {Component, pageProps} = this.props;

        return (
            <>
                <Head>
                    <title>{process.env.title}</title>
P
Peter Pan 已提交
56
                    <link rel="shortcut icon" href={`${process.env.PUBLIC_PATH}/favicon.ico`} />
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
                    <meta
                        name="viewport"
                        content="width=device-width,minimum-scale=1,maximum-scale=1,initial-scale=1,user-scalable=no,shrink-to-fit=no"
                    />
                    <meta name="description" content={process.env.description} />
                    <meta name="keywords" content={process.env.keywords} />
                    <meta name="author" content={process.env.author} />
                </Head>
                <GlobalStyle />
                <SWRConfig
                    value={{
                        fetcher,
                        revalidateOnFocus: false,
                        revalidateOnReconnect: false
                    }}
                >
                    <Layout>
                        <Component {...pageProps} />
                    </Layout>
76
                    <ToastContainer position="top-center" hideProgressBar closeOnClick={false} />
77 78 79 80
                </SWRConfig>
            </>
        );
    }
81 82 83 84 85 86 87 88 89

    static async getInitialProps(appContext: AppContext) {
        const appProps = await App.getInitialProps(appContext);

        if (process.env.API_TOKEN_KEY) {
            setApiToken(appContext.router.query[process.env.API_TOKEN_KEY]);
        }
        return {...appProps};
    }
90 91
}

92
export default appWithTranslation(withRouter(VDLApp));