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

Commit 33f47af

Browse files
committed
very dirty Just Working i18n for most cases
1 parent bd3a8c4 commit 33f47af

File tree

4 files changed

+141
-23
lines changed

4 files changed

+141
-23
lines changed

lib/pages/getServerSideProps/redirects.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,27 @@
11
const getNetlifyFunctionName = require("../../helpers/getNetlifyFunctionName");
22
const getDataRouteForRoute = require("../../helpers/getDataRouteForRoute");
33
const pages = require("./pages");
4+
const getNextConfig = require("../../helpers/getNextConfig");
45

56
const redirects = [];
67

8+
const nextConfig = getNextConfig();
9+
710
pages.forEach(({ route, filePath }) => {
811
const functionName = getNetlifyFunctionName(filePath);
912

13+
// If i18n, need to accommodate for locales
14+
if (nextConfig.i18n) {
15+
const { locales } = nextConfig.i18n;
16+
// Add redirects for each locale
17+
locales.forEach((locale) => {
18+
redirects.push({
19+
route: `/${locale}${route}`,
20+
target: `/.netlify/functions/${functionName}`,
21+
});
22+
});
23+
}
24+
1025
// Add one redirect for the page
1126
redirects.push({
1227
route,

lib/pages/getStaticProps/redirects.js

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const pages = require("./pages");
99
// redirects for handling preview mode.
1010
const redirects = [];
1111

12+
const defaultLocaleRedirects = [];
13+
const dynamicLocalesAdded = [];
14+
1215
pages.forEach(({ route, dataRoute, srcRoute }) => {
1316
const relativePath = getFilePathForRoute(srcRoute || route, "js");
1417
const filePath = join("pages", relativePath);
@@ -19,23 +22,65 @@ pages.forEach(({ route, dataRoute, srcRoute }) => {
1922

2023
const nextConfig = getNextConfig();
2124
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}`;
25+
const { defaultLocale, locales } = nextConfig.i18n;
26+
const isNotDynamic = !srcRoute;
27+
if (isNotDynamic) {
28+
if (defaultLocale) {
29+
redirects.push({
30+
route,
31+
target: `/${defaultLocale}${route}`,
32+
});
33+
}
34+
locales.forEach((locale) => {
35+
const pageRoute = `/${locale}${route}`;
36+
redirects.push({
37+
route: pageRoute,
38+
target,
39+
force: true,
40+
conditions,
41+
});
42+
});
43+
// Data routes are consistent across locale so it can be outside of the loop
2644
redirects.push({
27-
route: pageRoute,
45+
route: dataRoute,
2846
target,
2947
force: true,
3048
conditions,
3149
});
50+
} else {
51+
if (defaultLocale && !defaultLocaleRedirects.includes(srcRoute)) {
52+
// Add redirect for /normal/:id -> /defaultLocale/normal/:id
53+
redirects.push({
54+
route: srcRoute,
55+
target: `/${defaultLocale}${srcRoute
56+
.replace("[", ":")
57+
.replace("]", "")}`,
58+
});
59+
defaultLocaleRedirects.push(srcRoute);
60+
}
61+
const routeLocale = route.split("/")[1];
62+
const srcRouteWithLocale = `/${routeLocale}${srcRoute}`;
63+
if (!dynamicLocalesAdded.includes(srcRouteWithLocale)) {
64+
// Add one redirect for the page, but only when the NextJS
65+
// preview mode cookies are present
66+
redirects.push({
67+
route: srcRouteWithLocale,
68+
target,
69+
force: true,
70+
conditions,
71+
});
72+
73+
dynamicLocalesAdded.push(srcRouteWithLocale);
74+
}
75+
76+
// Add one redirect for the data route, same conditions
3277
redirects.push({
33-
route: dataRoute_,
78+
route: dataRoute,
3479
target,
3580
force: true,
3681
conditions,
3782
});
38-
});
83+
}
3984
} else {
4085
// Add one redirect for the page, but only when the NextJS
4186
// preview mode cookies are present

lib/pages/getStaticProps/setup.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ const setup = ({ functionsPath, publishPath }) => {
2525

2626
const nextConfig = getNextConfig();
2727

28-
// If an app is using Next 10+'s i18n feature, next will put statically
28+
// If an app is using i18n, Next will put statically
2929
// generated files under a different path for each different locale
30+
// i.e. pages/static -> pages/{locale}/static
3031
if (nextConfig.i18n) {
31-
const { locales } = nextConfig.i18n;
32-
if (!locales || locales.length === 0) return;
32+
const { locales = [] } = nextConfig.i18n;
3333

34+
// For dynamic routes in i18n, Next auto-prepends with locale
35+
// in prerender-manifest; static routes are missing locale so
36+
// we need to manually accommodate
3437
const isNotDynamic = !srcRoute;
35-
// Dynamic routes don't need special helper, Next auto prepends with locale
36-
// in prerender-manifest, but static routes are missing locale
3738
if (isNotDynamic) {
3839
locales.forEach((locale) => {
3940
// Copy pre-rendered HTML page

lib/pages/getStaticPropsWithRevalidate/redirects.js

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,81 @@ const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
33
const getNetlifyFunctionName = require("../../helpers/getNetlifyFunctionName");
44
const pages = require("./pages");
55

6+
const getNextConfig = require("../../helpers/getNextConfig");
7+
68
const redirects = [];
79

10+
console.log("REVAL PAGES", pages);
11+
12+
const nextConfig = getNextConfig();
13+
14+
const srcRouteRedirectsAdded = [];
15+
const dynamicLocalesAdded = [];
16+
817
pages.forEach(({ route, srcRoute, dataRoute }) => {
918
const relativePath = getFilePathForRoute(srcRoute || route, "js");
1019
const filePath = join("pages", relativePath);
1120
const functionName = getNetlifyFunctionName(filePath);
21+
const target = `/.netlify/functions/${functionName}`;
22+
23+
if (nextConfig.i18n) {
24+
const { defaultLocale, locales } = nextConfig.i18n;
25+
const isNotDynamic = !srcRoute;
26+
if (isNotDynamic) {
27+
if (defaultLocale) {
28+
// lol... /reval -> /:function
29+
redirects.push({
30+
route,
31+
target,
32+
});
33+
}
34+
locales.forEach((locale) => {
35+
const pageRoute = `/${locale}${route}`;
36+
redirects.push({
37+
route: pageRoute,
38+
target,
39+
});
40+
});
41+
redirects.push({
42+
route: dataRoute,
43+
target,
44+
});
45+
} else {
46+
// Add a redirect for each srcRoute
47+
if (!srcRouteRedirectsAdded.includes(srcRoute)) {
48+
redirects.push({
49+
route: srcRoute,
50+
target,
51+
});
52+
redirects.push({
53+
route: dataRoute,
54+
target,
55+
});
56+
srcRouteRedirectsAdded.push(srcRoute);
57+
}
58+
59+
const routeLocale = route.split("/")[1];
60+
if (!dynamicLocalesAdded.includes(routeLocale)) {
61+
redirects.push({
62+
route: `/${routeLocale}${srcRoute}`,
63+
target,
64+
});
65+
dynamicLocalesAdded.push(routeLocale);
66+
}
67+
}
68+
} else {
69+
// Add one redirect for the page
70+
redirects.push({
71+
route,
72+
target,
73+
});
1274

13-
// Add one redirect for the page
14-
redirects.push({
15-
route,
16-
target: `/.netlify/functions/${functionName}`,
17-
});
18-
19-
// Add one redirect for the data route
20-
redirects.push({
21-
route: dataRoute,
22-
target: `/.netlify/functions/${functionName}`,
23-
});
75+
// Add one redirect for the data route
76+
redirects.push({
77+
route: dataRoute,
78+
target,
79+
});
80+
}
2481
});
2582

2683
module.exports = redirects;

0 commit comments

Comments
 (0)