67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
import Document, {
|
|
Html,
|
|
Head,
|
|
Main,
|
|
NextScript,
|
|
DocumentContext,
|
|
DocumentInitialProps,
|
|
} from 'next/document';
|
|
import { SWRConfig } from 'swr';
|
|
import * as React from 'react';
|
|
import { IntlProvider } from 'react-intl';
|
|
import { fetchJson } from '../util/api';
|
|
import withSession, { IncomingMessageWithSession } from '../util/session';
|
|
import { defaultLocale, loadLocaleData, LocaleData } from '../util/localization';
|
|
|
|
const getNewProps = withSession(async (req: IncomingMessageWithSession) => {
|
|
const enableDark = req.session.get('enable-dark') || false;
|
|
const locale = req.session.get('locale') || defaultLocale;
|
|
const messages = await loadLocaleData(locale);
|
|
|
|
return {
|
|
enableDark,
|
|
locale,
|
|
messages,
|
|
};
|
|
});
|
|
|
|
class GDQArchiveDocument extends Document<{
|
|
enableDark: boolean,
|
|
locale: string,
|
|
messages: LocaleData,
|
|
}> {
|
|
static async getInitialProps(ctx: DocumentContext): Promise<DocumentInitialProps> {
|
|
const initialProps = await Document.getInitialProps(ctx);
|
|
return {
|
|
...initialProps,
|
|
...await getNewProps(ctx.req, ctx.res),
|
|
};
|
|
}
|
|
|
|
render() {
|
|
const { enableDark, locale, messages } = this.props;
|
|
return (
|
|
<SWRConfig
|
|
value={{
|
|
fetcher: fetchJson,
|
|
onError: (err) => {
|
|
console.error(err);
|
|
},
|
|
}}
|
|
>
|
|
<IntlProvider messages={messages} locale={locale} defaultLocale={defaultLocale}>
|
|
<Html lang={locale} data-enable-dark={enableDark}>
|
|
<Head />
|
|
<body>
|
|
<Main />
|
|
<NextScript />
|
|
</body>
|
|
</Html>
|
|
</IntlProvider>
|
|
</SWRConfig>
|
|
);
|
|
}
|
|
}
|
|
|
|
export default GDQArchiveDocument;
|