Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit 5f34aa1

Browse files
committed
rough pass at i18n ssg breaking changes in next 10
1 parent a48735f commit 5f34aa1

File tree

9 files changed

+1320
-1010
lines changed

9 files changed

+1320
-1010
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Next apps with i18n have statically generated html for/under each locale
2+
const getFilePathForRoute = require("./getFilePathForRoute");
3+
4+
const getFilePathForRouteWithI18n = (route, extension, locale) => {
5+
return `${locale}${getFilePathForRoute(route, extension)}`;
6+
};
7+
8+
module.exports = getFilePathForRouteWithI18n;

lib/helpers/getNextConfig.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Get next.config.js
2+
const { PHASE_PRODUCTION_BUILD } = require("next/constants");
3+
const { default: loadConfig } = require("next/dist/next-server/server/config");
4+
const { resolve } = require("path");
5+
6+
const getNextConfig = () => {
7+
// Load next.config.js
8+
// Use same code as https://github.com/vercel/next.js/blob/25488f4a03db30cade4d086ba49cd9a50a2ac02e/packages/next/build/index.ts#L114
9+
return loadConfig(PHASE_PRODUCTION_BUILD, resolve("."));
10+
};
11+
12+
module.exports = getNextConfig;

lib/helpers/getNextDistDir.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
// Get the NextJS distDir specified in next.config.js
2-
const { PHASE_PRODUCTION_BUILD } = require("next/constants");
3-
const { default: loadConfig } = require("next/dist/next-server/server/config");
4-
const { resolve, join } = require("path");
2+
const { join } = require("path");
3+
const getNextConfig = require("./getNextConfig");
54

65
const getNextDistDir = ({ nextConfigPath }) => {
7-
// Load next.config.js
8-
// Use same code as https://github.com/vercel/next.js/blob/25488f4a03db30cade4d086ba49cd9a50a2ac02e/packages/next/build/index.ts#L114
9-
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, resolve("."));
6+
const nextConfig = getNextConfig();
107

118
return join(".", nextConfig.distDir);
129
};

lib/pages/getStaticProps/redirects.js

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const { join } = require("path");
22
const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
3+
const getFilePathForRouteWithI18n = require("../../helpers/getFilePathForRouteWithI18n");
34
const getNetlifyFunctionName = require("../../helpers/getNetlifyFunctionName");
5+
const getNextConfig = require("../../helpers/getNextConfig");
46
const pages = require("./pages");
57

68
// Pages with getStaticProps (without fallback or revalidation) only need
@@ -15,22 +17,43 @@ pages.forEach(({ route, dataRoute, srcRoute }) => {
1517
const conditions = ["Cookie=__prerender_bypass,__next_preview_data"];
1618
const target = `/.netlify/functions/${functionName}`;
1719

18-
// Add one redirect for the page, but only when the NextJS
19-
// preview mode cookies are present
20-
redirects.push({
21-
route,
22-
target,
23-
force: true,
24-
conditions,
25-
});
20+
const nextConfig = getNextConfig();
21+
if (nextConfig.i18n) {
22+
const { locales } = nextConfig.i18n;
23+
locales.forEach((locale) => {
24+
const pageRoute = srcRoute ? route : `${locale}${route}`;
25+
const dataRoute_ = srcRoute ? dataRoute : `${locale}${dataRoute}`;
26+
redirects.push({
27+
route: pageRoute,
28+
target,
29+
force: true,
30+
conditions,
31+
});
32+
redirects.push({
33+
route: dataRoute_,
34+
target,
35+
force: true,
36+
conditions,
37+
});
38+
});
39+
} else {
40+
// Add one redirect for the page, but only when the NextJS
41+
// preview mode cookies are present
42+
redirects.push({
43+
route,
44+
target,
45+
force: true,
46+
conditions,
47+
});
2648

27-
// Add one redirect for the data route, same conditions
28-
redirects.push({
29-
route: dataRoute,
30-
target,
31-
force: true,
32-
conditions,
33-
});
49+
// Add one redirect for the data route, same conditions
50+
redirects.push({
51+
route: dataRoute,
52+
target,
53+
force: true,
54+
conditions,
55+
});
56+
}
3457
});
3558

3659
module.exports = redirects;

lib/pages/getStaticProps/setup.js

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const { join } = require("path");
22
const { logTitle, logItem } = require("../../helpers/logger");
33
const { NETLIFY_PUBLISH_PATH } = require("../../config");
44
const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
5+
const getFilePathForRouteWithI18n = require("../../helpers/getFilePathForRouteWithI18n");
6+
const getNextConfig = require("../../helpers/getNextConfig");
57
const isRouteWithFallback = require("../../helpers/isRouteWithFallback");
68
const setupStaticFileForPage = require("../../helpers/setupStaticFileForPage");
79
const setupNetlifyFunctionForPage = require("../../helpers/setupNetlifyFunctionForPage");
@@ -21,13 +23,56 @@ const setup = () => {
2123
pages.forEach(({ route, dataRoute, srcRoute }) => {
2224
logItem(route);
2325

24-
// Copy pre-rendered HTML page
25-
const htmlPath = getFilePathForRoute(route, "html");
26-
setupStaticFileForPage(htmlPath);
26+
const nextConfig = getNextConfig();
2727

28-
// Copy page's JSON data
29-
const jsonPath = getFilePathForRoute(route, "json");
30-
setupStaticFileForPage(jsonPath, dataRoute);
28+
// If an app is using Next 10+'s i18n feature, next will put statically
29+
// generated files under a different path for each different locale
30+
if (nextConfig.i18n) {
31+
const { locales } = nextConfig.i18n;
32+
if (!locales || locales.length === 0) return;
33+
34+
// ok so you dont need to loop over for srcRoutes and do the
35+
// set up twice
36+
const isNotDynamic = !srcRoute;
37+
// Dynamic routes dont need special helper, Next auto prepends with locale
38+
// in prerender-manifest
39+
if (isNotDynamic) {
40+
locales.forEach((locale) => {
41+
// Copy pre-rendered HTML page
42+
const htmlPath = getFilePathForRouteWithI18n(route, "html", locale);
43+
setupStaticFileForPage(htmlPath);
44+
});
45+
// Copy page's JSON data
46+
// TO-DO: get more clarity on dataRoute logic/files;
47+
// dataRoute is the same for both/all locales (as is route above)
48+
// BUT in setupStaticFileForPage we use the second arg as the outputhPath
49+
// (unlike the html pages above where we use the first arg/route as the outputPath)
50+
// and its not clear why.. but assuming we only have/need this
51+
// one json we dont need to do in the locale loop/for each locale
52+
const jsonPath = getFilePathForRouteWithI18n(
53+
route,
54+
"json",
55+
nextConfig.i18n.defaultLocale || locales[0]
56+
);
57+
setupStaticFileForPage(jsonPath, dataRoute);
58+
} else {
59+
// Copy pre-rendered HTML page
60+
const htmlPath = getFilePathForRoute(route, "html");
61+
setupStaticFileForPage(htmlPath);
62+
63+
// Copy page's JSON data
64+
const jsonPath = getFilePathForRoute(route, "json");
65+
setupStaticFileForPage(jsonPath, dataRoute);
66+
}
67+
} else {
68+
// Copy pre-rendered HTML page
69+
const htmlPath = getFilePathForRoute(route, "html");
70+
setupStaticFileForPage(htmlPath);
71+
72+
// Copy page's JSON data
73+
const jsonPath = getFilePathForRoute(route, "json");
74+
setupStaticFileForPage(jsonPath, dataRoute);
75+
}
3176

3277
// // Set up the Netlify function (this is ONLY for preview mode)
3378
const relativePath = getFilePathForRoute(srcRoute || route, "js");

0 commit comments

Comments
 (0)