|
| 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; |
0 commit comments