-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path_app.js
63 lines (53 loc) · 2.27 KB
/
_app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import universalLanguageDetect from '@unly/universal-language-detector';
import get from 'lodash.get';
import NextCookies from 'next-cookies';
import NextApp from 'next/app';
import React from 'react';
import { FALLBACK_LANG, SUPPORTED_LANGUAGES } from '../utils/i18n';
class App extends NextApp {
static async getInitialProps(props) {
const { ctx } = props;
const { req } = ctx;
const cookies = NextCookies(ctx); // Parses Next.js cookies in a universal way (server + client) - It's an object
// Universally detects the user's language
const lang = universalLanguageDetect({
supportedLanguages: SUPPORTED_LANGUAGES, // Whitelist of supported languages, will be used to filter out languages that aren't supported
fallbackLanguage: FALLBACK_LANG, // Fallback language in case the user's language cannot be resolved
acceptLanguageHeader: get(req, 'headers.accept-language'), // Optional - Accept-language header will be used when resolving the language on the server side
serverCookies: cookies, // Optional - Cookie "i18next" takes precedence over navigator configuration (ex: "i18next: fr"), will only be used on the server side
errorHandler: (error, level, origin, context) => { // Optional - Use you own logger here, Sentry, etc.
console.log('Custom error handler:');
console.error(error);
// Example if using Sentry in your app:
// Sentry.withScope((scope): void => {
// scope.setExtra('level', level);
// scope.setExtra('origin', origin);
// scope.setContext('context', context);
// Sentry.captureException(error);
// });
},
});
console.log('lang', lang)
// Calls page's `getInitialProps` and fills `appProps.pageProps` - XXX See https://nextjs.org/docs#custom-app
const appProps = await NextApp.getInitialProps(props);
appProps.pageProps = {
...appProps.pageProps,
cookies, // Object containing all cookies
lang, // i.e: 'en'
isSSR: !!req,
};
return { ...appProps };
}
render() {
const { Component, pageProps, router, err } = this.props;
const modifiedPageProps = {
...pageProps,
err,
router,
};
return (
<Component {...modifiedPageProps} />
);
}
}
export default App;