In an effort to facilitate the nextjs upgrade (12 -> 14), it's become unclear how to configure i18n in an SSG app only using dynamic catch all routes leveraging the pages router.
The crux of the problem is NextJS is now erroring for SSG when setting i18n in config. When it's removed and locales are side loaded in getStaticPaths, the build errors out indicating locale is not specified in config.
Can someone please point to the changes necessary to facilitate this upgrade? next-translate is currently used to handle i18n but is not a requirement to maintain; SSG, i18n, the pages router and dynamic catch all routes are.
next.config.js:
const nextConfig = {
output: 'export',
i18n: {
locales, defaultLocale,
localeDetection: false,
},
./src/pages/[[...slug]].js (dynamic catch all route):
export const getStaticPaths = async ({ locales }) => {
const paths = [];
await Promise.all(locales?.map(async (locale) => {
// fetch and parse entries, then:
paths.push({ params: { slug }, locale });
}));
return {
paths,
fallback: false
};
};
i18n.js:
module.exports={
"localeDetection": false,
"locales": [
"en-US",
"es"
],
"defaultLocale": "en-US",
"loadLocaleFrom": (lang, ns) => import(`./locales/${lang}/${ns}.json`).then((m) => m.default),
"pages": {
"*": [
"common"
]
},
"logBuild": false
};
Versions:
- node: 20.11.0
- next: 14.1.0
- react: 18.2.0
- next-translate: 2.6.2
Given the previous/original next.config.js, the build fails, logging i18n cannot be specified in next.config.js if output: 'export' is also set. Yet, removing i18n surfaces issues w/in getStaticProps:
localesdoes not exist in NextJS cache, given if noi18nis set in config. Previously,locales(from NextJS context) was mapped over as a part ofpathsgeneration, e.g.getStaticPathssnippet above.Attempting to side-load
localesw/ingetStaticPaths, e.g.:
import i18n from '../../i18n';
export const getStaticPaths = async () => {
const { locales } = i18n;
Yields the following error:
Error: Invalid locale returned from getStaticPaths for /[[...slug]], the locale en-US is not specified in next.config.js
Build is completing when using
patch-packageto comment out two errors relating toi18nuse w/ SSG. In effect, this patch circumvents seemingly needless restrictions imposed by NextJS (at least onv13.x.x) to prevent i18n with static export.next+13.4.9.patchnext.config.js:Versions used in current working build (changed from those specified in the question):