Skip to content

Commit 4b8900f

Browse files
committed
Disable SSR entirely (_app) and use SSG with basic i18N + update to next 9.3.6
1 parent 0c88c2c commit 4b8900f

File tree

5 files changed

+1215
-480
lines changed

5 files changed

+1215
-480
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
"lodash.map": "4.6.0",
9595
"lodash.remove": "4.7.0",
9696
"lodash.xorby": "4.7.0",
97-
"next": "9.3.4",
97+
"next": "9.3.6",
9898
"next-cookies": "2.0.3",
9999
"next-with-apollo": "5.0.0",
100100
"prop-types": "15.7.2",

src/pages/[lang]/index.tsx

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/** @jsx jsx */
2+
import { jsx } from '@emotion/core';
3+
import * as Sentry from '@sentry/node';
4+
import { isBrowser } from '@unly/utils';
5+
import { createLogger } from '@unly/utils-simple-logger';
6+
import { GetStaticPaths, GetStaticProps, NextPage } from 'next';
7+
import Link from 'next/link';
8+
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-unused-vars
9+
import React from 'react';
10+
import { PageProps } from '../../types/PageProps';
11+
12+
const fileLabel = 'pages/index';
13+
const logger = createLogger({ // eslint-disable-line no-unused-vars,@typescript-eslint/no-unused-vars
14+
label: fileLabel,
15+
});
16+
17+
const Home: NextPage<PageProps> = (props: Props): JSX.Element => {
18+
Sentry.addBreadcrumb({ // See https://docs.sentry.io/enriching-error-data/breadcrumbs
19+
category: fileLabel,
20+
message: `Rendering index page (${isBrowser() ? 'browser' : 'server'})`,
21+
level: Sentry.Severity.Debug,
22+
});
23+
const { lang } = props;
24+
25+
console.log('Home props', props);
26+
console.log('lang', lang);
27+
28+
return (
29+
<div>
30+
<h1>{lang}</h1>
31+
32+
<Link href={'/fr/terms'} passHref>
33+
<a>Terms</a>
34+
</Link>
35+
</div>
36+
);
37+
};
38+
39+
type Props = {
40+
lang: string;
41+
isStaticRendering: boolean;
42+
} & PageProps;
43+
44+
/**
45+
* Only executed on the server side at build time.
46+
*
47+
* When a page uses "getStaticProps", then "_app:getInitialProps" is executed but not actually used by the page,
48+
* only the results from getStaticProps are used.
49+
*
50+
* @param context
51+
* @return Props that will be passed to the Page component, as props
52+
*
53+
* @see https://github.com/zeit/next.js/discussions/10949#discussioncomment-6884
54+
* @see https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation
55+
*/
56+
// export const getStaticProps: GetStaticProps = async (context: GetStaticPropsContext): Promise<{ props: any }> => {
57+
// console.log('getStaticProps', context);
58+
//
59+
// const lang: string = universalLanguageDetect({
60+
// supportedLanguages: SUPPORTED_LANGUAGES, // Whitelist of supported languages, will be used to filter out languages that aren't supported
61+
// fallbackLanguage: LANG_EN, // Fallback language in case the user's language cannot be resolved
62+
// acceptLanguageHeader: null,
63+
// serverCookies: null,
64+
// errorHandler: (error: Error, level: ERROR_LEVELS, origin: string, context: object): void => {
65+
// Sentry.withScope((scope): void => {
66+
// scope.setExtra('level', level);
67+
// scope.setExtra('origin', origin);
68+
// scope.setContext('context', context);
69+
// Sentry.captureException(error);
70+
// });
71+
// logger.error(error.message);
72+
// },
73+
// });
74+
// const bestCountryCodes: string[] = [lang, resolveFallbackLanguage(lang)];
75+
// const gcmsLocales: string = prepareGraphCMSLocaleHeader(bestCountryCodes);
76+
// const defaultLocales: I18nextResources = await fetchTranslations(lang); // Pre-fetches translations from Locize API
77+
//
78+
// return {
79+
// props: {
80+
// customerRef: process.env.CUSTOMER_REF,
81+
// isSSRReadyToRender: true,
82+
// lang,
83+
// bestCountryCodes,
84+
// gcmsLocales,
85+
// defaultLocales,
86+
// },
87+
// };
88+
89+
// return {
90+
// props: {},
91+
// };
92+
// };
93+
94+
export const getStaticProps: GetStaticProps<any, any> = async (props) => {
95+
console.log('getStaticProps', props);
96+
return {
97+
props: {
98+
lang: props?.params?.lang,
99+
isStaticRendering: true,
100+
},
101+
// revalidate: false,
102+
};
103+
};
104+
105+
export const getStaticPaths: GetStaticPaths = async () => {
106+
return {
107+
paths: [{ params: { lang: 'fr' } }, { params: { lang: 'en' } }],
108+
fallback: false,
109+
};
110+
};
111+
112+
export default Home;

src/pages/[lang]/terms.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/** @jsx jsx */
2+
import { jsx } from '@emotion/core';
3+
import { createLogger } from '@unly/utils-simple-logger';
4+
import { GetStaticPaths, GetStaticProps, NextPage } from 'next';
5+
import Link from 'next/link';
6+
// eslint-disable-next-line @typescript-eslint/no-unused-vars,no-unused-vars
7+
import React from 'react';
8+
import { PageProps } from '../../types/PageProps';
9+
10+
const fileLabel = 'pages/terms';
11+
const logger = createLogger({ // eslint-disable-line no-unused-vars,@typescript-eslint/no-unused-vars
12+
label: fileLabel,
13+
});
14+
15+
const Terms: NextPage<PageProps> = (props: Props): JSX.Element => {
16+
const { lang } = props;
17+
console.log('Terms props', props);
18+
console.log('lang', lang);
19+
20+
return (
21+
<div>
22+
<h1>{lang}</h1>
23+
24+
<Link href={'/fr'} passHref>
25+
<a>Index</a>
26+
</Link>
27+
</div>
28+
);
29+
};
30+
31+
type Props = {
32+
lang: string;
33+
isStaticRendering: boolean;
34+
} & PageProps;
35+
36+
export const getStaticProps: GetStaticProps<any, any> = async (props) => {
37+
console.log('getStaticProps', props);
38+
return {
39+
props: {
40+
lang: props?.params?.lang,
41+
isStaticRendering: true,
42+
},
43+
// revalidate: false,
44+
};
45+
};
46+
47+
export const getStaticPaths: GetStaticPaths = async () => {
48+
return {
49+
paths: [{ params: { lang: 'fr' } }, { params: { lang: 'en' } }],
50+
fallback: false,
51+
};
52+
};
53+
54+
export default Terms;

0 commit comments

Comments
 (0)