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

Commit e69671d

Browse files
committed
missing initialProps + fallback logic + PR feedback
1 parent 447426d commit e69671d

File tree

17 files changed

+152
-138
lines changed

17 files changed

+152
-138
lines changed

cypress/integration/i18n_spec.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -750,35 +750,16 @@ describe("i18n-specific behavior", () => {
750750
cy.get("h1").should("contain", "Show #5");
751751
});
752752

753-
it("does render an included path for fallback routes with non-default locale defined", () => {
753+
it("renders an included path for fallback routes with non-default locale defined", () => {
754754
cy.visit("/fr/getStaticProps/withFallback/4");
755755

756756
cy.get("h1").should("contain", "Show #4");
757757
});
758758

759-
it("does not render an included path for fallback routes with non-default locale", () => {
760-
// because the locale needs to be included in the paths provided to getStaticPaths
761-
cy.request({
762-
url: "/fr/getStaticProps/withFallback/3",
763-
failOnStatusCode: false,
764-
}).then((response) => {
765-
expect(response.status).to.eq(404);
766-
cy.state("document").write(response.body);
767-
});
768-
769-
cy.get("h2").should("contain", "This page could not be found.");
770-
});
759+
it("renders non-included path for fallback routes with non-default locale", () => {
760+
cy.visit("/fr/getStaticProps/withFallback/71");
771761

772-
it("does not render a not-included path for fallback routes with non-default locale", () => {
773-
cy.request({
774-
url: "/fr/getStaticProps/withFallback/5",
775-
failOnStatusCode: false,
776-
}).then((response) => {
777-
expect(response.status).to.eq(404);
778-
cy.state("document").write(response.body);
779-
});
780-
781-
cy.get("h2").should("contain", "This page could not be found.");
762+
cy.get("h1").should("contain", "Show #71");
782763
});
783764

784765
it("renders non-default locale path for revalidate page", () => {
@@ -803,13 +784,13 @@ describe("i18n-specific behavior", () => {
803784
});
804785

805786
context("SSR'd pages", () => {
806-
it("renders non-default locale for SSR'd non-dynamic route", () => {
787+
it("renders non-default locale for non-dynamic route", () => {
807788
cy.visit("/fr/getServerSideProps/static");
808789

809790
cy.get("h1").should("contain", "Show #42");
810791
});
811792

812-
it("renders non-default locale for SSR'd dynamic route", () => {
793+
it("renders non-default locale for dynamic route", () => {
813794
cy.visit("/fr/getServerSideProps/5");
814795

815796
cy.get("h1").should("contain", "Show #5");
@@ -821,4 +802,30 @@ describe("i18n-specific behavior", () => {
821802
cy.get("h1").should("contain", "Show #5");
822803
});
823804
});
805+
806+
context("getInitialProps pages", () => {
807+
it("renders naked route", () => {
808+
cy.visit("/shows/42");
809+
810+
cy.get("h1").should("contain", "Show #42");
811+
});
812+
813+
it("renders non-default locale", () => {
814+
cy.visit("/fr/shows/42");
815+
816+
cy.get("h1").should("contain", "Show #42");
817+
});
818+
});
819+
820+
context("withoutProps pages", () => {
821+
it("renders default locale", () => {
822+
cy.visit("/en/static");
823+
cy.get("p").should("contain", "It is a static page.");
824+
});
825+
826+
it("renders non-default locale", () => {
827+
cy.visit("/fr/static");
828+
cy.get("p").should("contain", "It is a static page.");
829+
});
830+
});
824831
});

lib/helpers/getDefaultLocaleRedirectForI18n.js renamed to lib/helpers/addDefaultLocaleRedirect.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ const { defaultLocale } = i18n;
55
// to the defaultLocale-prepended route i.e. /static -> /en/static
66
// Note: there can only one defaultLocale, but we put it in an array to simplify
77
// logic in redirects.js files via concatenation
8-
const getDefaultLocaleRedirectForI18n = (route, srcRoute, target) => {
8+
const addDefaultLocaleRedirect = (redirects) => (
9+
route,
10+
target,
11+
additionalParams
12+
) => {
913
// If no i18n, skip
10-
if (!defaultLocale) return [];
14+
if (!defaultLocale) return;
1115

1216
const routePieces = route.split("/");
1317
const routeLocale = routePieces[1];
1418
if (routeLocale === defaultLocale) {
1519
const nakedRoute =
1620
route === `/${routeLocale}` ? "/" : route.replace(`/${routeLocale}`, "");
17-
// const nakedRoute = routePieces.slice(2, routePieces.length).join("/");
18-
return [
19-
{
20-
route: nakedRoute,
21-
target: target || route,
22-
},
23-
];
21+
redirects.push({
22+
route: nakedRoute,
23+
target: target || route,
24+
...additionalParams,
25+
});
2426
}
25-
26-
return [];
2727
};
2828

29-
module.exports = getDefaultLocaleRedirectForI18n;
29+
module.exports = addDefaultLocaleRedirect;

lib/helpers/addLocaleRedirects.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const i18n = require("./getI18n")();
2+
const getDataRouteForRoute = require("./getDataRouteForRoute");
3+
4+
const addLocaleRedirects = (redirects) => (route, target) => {
5+
i18n.locales.forEach((locale) => {
6+
redirects.push({
7+
route: `/${locale}${route === "/" ? "" : route}`,
8+
target,
9+
});
10+
redirects.push({
11+
route: getDataRouteForRoute(route, locale),
12+
target,
13+
});
14+
});
15+
};
16+
17+
module.exports = addLocaleRedirects;

lib/helpers/getDataRouteForRoute.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ const getFilePathForRoute = require("./getFilePathForRoute");
77
const fileContents = readFileSync(join(NEXT_DIST_DIR, "BUILD_ID"));
88
const buildId = fileContents.toString();
99

10-
// Return the data route for the given route
11-
const getDataRouteForRoute = (route, locale) => {
10+
const getPlainDataRoute = (route) => {
1211
const filePath = getFilePathForRoute(route, "json");
13-
14-
if (locale) return `/_next/data/${buildId}/${locale}${filePath}`;
1512
return `/_next/data/${buildId}${filePath}`;
1613
};
1714

15+
const getI18nDataRoute = (route, locale) => {
16+
const filePath = getFilePathForRoute(route, "json");
17+
return route === "/"
18+
? getPlainDataRoute(`/${locale}`)
19+
: `/_next/data/${buildId}/${locale}${filePath}`;
20+
};
21+
22+
// Return the data route for the given route
23+
const getDataRouteForRoute = (route, locale) => {
24+
if (locale) return getI18nDataRoute(route, locale);
25+
return getPlainDataRoute(route);
26+
};
27+
1828
module.exports = getDataRouteForRoute;

lib/helpers/getLocaleRoutesForI18n.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
const i18n = require("./getI18n")();
22
const getDataRouteForRoute = require("./getDataRouteForRoute");
33

4-
const getI18nDataRoute = (route, locale) => {
5-
return route === "/"
6-
? getDataRouteForRoute(`/${locale}`)
7-
: getDataRouteForRoute(route, locale);
8-
};
9-
104
// In i18n projects, Next does not prepend static routes with locales
115
// so we have to do it manually
126
const getLocaleRoutesForI18n = ({ route, srcRoute }) => {
@@ -15,8 +9,7 @@ const getLocaleRoutesForI18n = ({ route, srcRoute }) => {
159
return i18n.locales.map((locale) => {
1610
return {
1711
route: `/${locale}${route}`,
18-
dataRoute: getI18nDataRoute(route, locale),
19-
nakedRoute: route,
12+
dataRoute: getDataRouteForRoute(route, locale),
2013
};
2114
});
2215
};

lib/helpers/getPrerenderManifest.js

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const transformManifestForI18n = (manifest) => {
99
const { defaultLocale, locales } = nextConfig.i18n;
1010
const newRoutes = {};
1111
Object.entries(routes).forEach(
12-
([route, { dataRoute, initialRevalidateSeconds, srcRoute }]) => {
12+
([route, { dataRoute, srcRoute, ...params }]) => {
1313
const isDynamicRoute = !!srcRoute;
1414
if (isDynamicRoute) {
1515
newRoutes[route] = routes[route];
@@ -19,22 +19,14 @@ const transformManifestForI18n = (manifest) => {
1919
newRoutes[routeWithPrependedLocale] = {
2020
dataRoute: getDataRouteForRoute(route, locale),
2121
srcRoute: route,
22-
initialRevalidateSeconds: false,
22+
...params,
2323
};
2424
});
2525
}
2626
}
2727
);
28-
const newDynamicRoutes = {};
29-
Object.entries(dynamicRoutes).forEach(([route, { dataRoute, fallback }]) => {
30-
newDynamicRoutes[route] = {
31-
route,
32-
dataRoute: getDataRouteForRoute(route, defaultLocale),
33-
fallback,
34-
};
35-
});
3628

37-
return { ...manifest, routes: newRoutes, dynamicRoutes: newDynamicRoutes };
29+
return { ...manifest, routes: newRoutes };
3830
};
3931

4032
const getPrerenderManifest = () => {
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
const addLocaleRedirects = require("../../helpers/addLocaleRedirects");
12
const getNetlifyFunctionName = require("../../helpers/getNetlifyFunctionName");
23
const pages = require("./pages");
34

4-
const redirects = pages.map(({ route, filePath }) => ({
5-
route,
6-
target: `/.netlify/functions/${getNetlifyFunctionName(filePath)}`,
7-
}));
5+
const redirects = [];
6+
7+
pages.forEach(({ route, filePath }) => {
8+
const functionName = getNetlifyFunctionName(filePath);
9+
const target = `/.netlify/functions/${functionName}`;
10+
11+
addLocaleRedirects(redirects)(route, target);
12+
13+
redirects.push({
14+
route,
15+
target,
16+
});
17+
});
818

919
module.exports = redirects;

lib/pages/getServerSideProps/redirects.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
const addLocaleRedirects = require("../../helpers/addLocaleRedirects");
12
const getNetlifyFunctionName = require("../../helpers/getNetlifyFunctionName");
23
const getDataRouteForRoute = require("../../helpers/getDataRouteForRoute");
3-
const getLocaleRoutesForI18n = require("../../helpers/getLocaleRoutesForI18n");
44
const pages = require("./pages");
55

66
const redirects = [];
@@ -17,17 +17,7 @@ pages.forEach(({ route, filePath }) => {
1717
const functionName = getNetlifyFunctionName(filePath);
1818
const target = `/.netlify/functions/${functionName}`;
1919

20-
// Add any locale redirects if i18n
21-
getLocaleRoutesForI18n({ route }).forEach((localeRoute) => {
22-
redirects.push({
23-
route: localeRoute.route,
24-
target,
25-
});
26-
redirects.push({
27-
route: localeRoute.dataRoute,
28-
target,
29-
});
30-
});
20+
addLocaleRedirects(redirects)(route, target);
3121

3222
// Add one redirect for the naked route
3323
// i.e. /ssr

lib/pages/getStaticProps/pages.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const getPrerenderManifest = require("../../helpers/getPrerenderManifest");
2-
const getLocaleRoutesForI18n = require("../../helpers/getLocaleRoutesForI18n");
32

43
// Collect pages
5-
let pages = [];
4+
const pages = [];
65

76
// Get pages using getStaticProps
87
const { routes } = getPrerenderManifest();

lib/pages/getStaticProps/redirects.js

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const { join } = require("path");
2+
const addDefaultLocaleRedirect = require("../../helpers/addDefaultLocaleRedirect");
23
const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
34
const getDataRouteForRoute = require("../../helpers/getDataRouteForRoute");
45
const getNetlifyFunctionName = require("../../helpers/getNetlifyFunctionName");
5-
const getDefaultLocaleRedirectForI18n = require("../../helpers/getDefaultLocaleRedirectForI18n");
66
const pages = require("./pages");
77

88
// Pages with getStaticProps (without fallback or revalidation) only need
@@ -47,21 +47,9 @@ pages.forEach(({ route, dataRoute, srcRoute }) => {
4747
...previewModeRedirect,
4848
});
4949

50-
const defaultLocaleRedirect = getDefaultLocaleRedirectForI18n(
51-
route,
52-
srcRoute
53-
);
54-
55-
// Add preview mode redirect for the naked route (*must* precede defaultLocale redirect)
56-
redirects = redirects.concat(
57-
defaultLocaleRedirect.map((r) => ({
58-
route: r.route,
59-
...previewModeRedirect,
60-
}))
61-
);
62-
63-
// Add redirect for naked route -> `${defaultLocale}/redirect` (if i18n)
64-
redirects = redirects.concat(defaultLocaleRedirect);
50+
// Preview mode default locale redirect must precede normal default locale redirect
51+
addDefaultLocaleRedirect(redirects)(route, target, previewModeRedirect);
52+
addDefaultLocaleRedirect(redirects)(route);
6553
});
6654

6755
module.exports = redirects;

lib/pages/getStaticProps/setup.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const setup = ({ functionsPath, publishPath }) => {
1919
// a function for the same file path twice
2020
const filePathsDone = [];
2121

22-
pages.forEach(({ route, dataRoute, srcRoute, nakedRoute }) => {
22+
pages.forEach(({ route, dataRoute, srcRoute }) => {
2323
logItem(route);
2424

2525
// Copy pre-rendered HTML page
@@ -35,7 +35,6 @@ const setup = ({ functionsPath, publishPath }) => {
3535
});
3636

3737
// Set up the Netlify function (this is ONLY for preview mode)
38-
// Use srcRoute if dynamic or nakedRoute if i18n & static || route for normal
3938
const relativePath = getFilePathForRoute(srcRoute || route, "js");
4039
const filePath = join("pages", relativePath);
4140

lib/pages/getStaticPropsWithFallback/redirects.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { join } = require("path");
2+
const addLocaleRedirects = require("../../helpers/addLocaleRedirects");
23
const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
34
const getNetlifyFunctionName = require("../../helpers/getNetlifyFunctionName");
45
const pages = require("./pages");
@@ -9,17 +10,20 @@ pages.forEach(({ route, dataRoute }) => {
910
const relativePath = getFilePathForRoute(route, "js");
1011
const filePath = join("pages", relativePath);
1112
const functionName = getNetlifyFunctionName(filePath);
13+
const target = `/.netlify/functions/${functionName}`;
14+
15+
addLocaleRedirects(redirects)(route, target);
1216

1317
// Add one redirect for the page
1418
redirects.push({
1519
route,
16-
target: `/.netlify/functions/${functionName}`,
20+
target,
1721
});
1822

1923
// Add one redirect for the data route
2024
redirects.push({
2125
route: dataRoute,
22-
target: `/.netlify/functions/${functionName}`,
26+
target,
2327
});
2428
});
2529

0 commit comments

Comments
 (0)