_app.tsx 2.8 KB
Newer Older
1
import {Router, appWithTranslation} from '~/utils/i18n';
2
import {fetcher, getApiToken} from '~/utils/fetch';
3

4
import App from 'next/app';
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 16
const API_TOKEN_KEY = process.env.API_TOKEN_KEY;
const PUBLIC_PATH = process.env.PUBLIC_PATH;
17

18
class VDLApp extends App {
19 20 21 22
    componentDidMount() {
        Router.events.on('routeChangeStart', () => NProgress.start());
        Router.events.on('routeChangeComplete', (url: string) => {
            NProgress.done();
23
            if (API_TOKEN_KEY) {
24 25
                const id = getApiToken();
                const parsed = queryString.parseUrl(url);
26
                if (id && !parsed.query[API_TOKEN_KEY]) {
27 28 29 30 31
                    this.props.router.replace(
                        queryString.stringifyUrl({
                            url: parsed.url,
                            query: {
                                ...parsed.query,
32
                                [API_TOKEN_KEY]: id
33 34 35 36 37 38 39 40 41 42 43
                            }
                        }),
                        undefined,
                        {shallow: true}
                    );
                }
            }
        });
        Router.events.on('routeChangeError', () => NProgress.done());
    }

44 45 46 47 48 49 50
    render() {
        const {Component, pageProps} = this.props;

        return (
            <>
                <Head>
                    <title>{process.env.title}</title>
51
                    <link rel="shortcut icon" href={`${PUBLIC_PATH}/favicon.ico`} />
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
                    <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>
71
                    <ToastContainer position="top-center" hideProgressBar closeOnClick={false} />
72 73 74 75 76 77
                </SWRConfig>
            </>
        );
    }
}

78
export default appWithTranslation(withRouter(VDLApp));