-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
SSG.ts
145 lines (137 loc) · 6.54 KB
/
SSG.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import map from 'lodash.map';
import { GetStaticPaths, GetStaticProps } from 'next';
import { supportedLocales } from '../../i18nConfig';
import { AirtableRecord } from '../../types/data/AirtableRecord';
import { Customer } from '../../types/data/Customer';
import { I18nLocale } from '../../types/i18n/I18nLocale';
import { CommonServerSideParams } from '../../types/nextjs/CommonServerSideParams';
import { PreviewData } from '../../types/nextjs/PreviewData';
import { StaticPath } from '../../types/nextjs/StaticPath';
import { StaticPathsOutput } from '../../types/nextjs/StaticPathsOutput';
import { StaticPropsInput } from '../../types/nextjs/StaticPropsInput';
import { StaticPropsOutput } from '../../types/nextjs/StaticPropsOutput';
import { SSGPageProps } from '../../types/pageProps/SSGPageProps';
import fetchCustomer from '../api/fetchCustomer';
import { DEFAULT_LOCALE, resolveFallbackLanguage } from '../i18n/i18n';
import { fetchTranslations, I18nextResources } from '../i18n/i18nextLocize';
/**
* Only executed on the server side at build time.
* Computes all static paths that should be available for all SSG pages
* Necessary when a page has dynamic routes and uses "getStaticProps", in order to build the HTML pages
*
* You can use "fallback" option to avoid building all page variants and allow runtime fallback
*
* Meant to avoid code duplication
* Can be overridden for per-page customisation (e.g: deepmerge)
*
* @return
*
* @see https://nextjs.org/docs/basic-features/data-fetching#getstaticpaths-static-generation
*/
export const getCommonStaticPaths: GetStaticPaths<CommonServerSideParams> = async (): Promise<StaticPathsOutput> => {
// TODO Make your own implementation.
// XXX Having this as separate function helps making your own pages without affecting existing examples under "pages/[locale]/examples".
// For instance, you may want to replace the Airtable query by your own API query, while keeping the existing example pages working.
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return getExamplesCommonStaticPaths();
};
/**
* Only executed on the server side at build time.
* Computes all static props that should be available for all SSG pages
*
* Note that when a page uses "getStaticProps", then "_app:getInitialProps" is executed (if defined) but not actually used by the page,
* only the results from getStaticProps are actually injected into the page (as "SSGPageProps").
*
* Meant to avoid code duplication
* Can be overridden for per-page customisation (e.g: deepmerge)
*
* @param props
* @return Props (as "SSGPageProps") that will be passed to the Page component, as props
*
* @see https://github.com/vercel/next.js/discussions/10949#discussioncomment-6884
* @see https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation
*/
export const getCommonStaticProps: GetStaticProps<SSGPageProps, CommonServerSideParams> = async (props: StaticPropsInput): Promise<StaticPropsOutput> => {
// TODO Make your own implementation.
// XXX Having this as separate function helps making your own pages without affecting existing examples under "pages/[locale]/examples".
// For instance, you may want to replace the Airtable query by your own API query, while keeping the existing example pages working.
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return getExamplesCommonStaticProps(props);
};
/**
* XXX This method is meant for people creating their own project based on NRN.
* It's meant to be deleted eventually when you don't need to keep the examples around.
*
* Only executed on the server side at build time.
* Computes all static paths that should be available for all SSG pages
* Necessary when a page has dynamic routes and uses "getStaticProps", in order to build the HTML pages
*
* You can use "fallback" option to avoid building all page variants and allow runtime fallback
*
* Meant to avoid code duplication
* Can be overridden for per-page customisation (e.g: deepmerge)
*
* @return
*
* @see https://nextjs.org/docs/basic-features/data-fetching#getstaticpaths-static-generation
*/
export const getExamplesCommonStaticPaths: GetStaticPaths<CommonServerSideParams> = async (): Promise<StaticPathsOutput> => {
const paths: StaticPath[] = map(supportedLocales, (supportedLocale: I18nLocale): StaticPath => {
return {
params: {
locale: supportedLocale.name,
},
};
});
return {
fallback: false,
paths,
};
};
/**
* XXX This method is meant for people creating their own project based on NRN.
* It's meant to be deleted eventually when you don't need to keep the examples around.
*
* Only executed on the server side at build time.
* Computes all static props that should be available for all SSG pages
*
* Note that when a page uses "getStaticProps", then "_app:getInitialProps" is executed (if defined) but not actually used by the page,
* only the results from getStaticProps are actually injected into the page (as "SSGPageProps").
*
* Meant to avoid code duplication
* Can be overridden for per-page customisation (e.g: deepmerge)
*
* @param props
* @return Props (as "SSGPageProps") that will be passed to the Page component, as props
*
* @see https://github.com/vercel/next.js/discussions/10949#discussioncomment-6884
* @see https://nextjs.org/docs/basic-features/data-fetching#getstaticprops-static-generation
*/
export const getExamplesCommonStaticProps: GetStaticProps<SSGPageProps, CommonServerSideParams> = async (props: StaticPropsInput): Promise<StaticPropsOutput> => {
const customerRef: string = process.env.NEXT_PUBLIC_CUSTOMER_REF;
const preview: boolean = props?.preview || false;
const previewData: PreviewData = props?.previewData || null;
const hasLocaleFromUrl = !!props?.params?.locale;
const locale: string = hasLocaleFromUrl ? props?.params?.locale : DEFAULT_LOCALE; // If the locale isn't found (e.g: 404 page)
const lang: string = locale.split('-')?.[0];
const bestCountryCodes: string[] = [lang, resolveFallbackLanguage(lang)];
const i18nTranslations: I18nextResources = await fetchTranslations(lang); // Pre-fetches translations from Locize API
const customer: AirtableRecord<Customer> = await fetchCustomer(bestCountryCodes);
return {
// Props returned here will be available as page properties (pageProps)
props: {
bestCountryCodes,
customer,
customerRef,
i18nTranslations,
hasLocaleFromUrl,
isReadyToRender: true,
isStaticRendering: true,
lang,
locale,
preview,
previewData,
},
// revalidate: false,
};
};