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

Commit 1b4cb5a

Browse files
committed
make i18n getDataRoute helper more DRY
1 parent 77dfc4e commit 1b4cb5a

File tree

3 files changed

+9
-15
lines changed

3 files changed

+9
-15
lines changed

.prettierignore

+3
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ jspm_packages
4343
# Optional REPL history
4444
.node_repl_history
4545
.next
46+
47+
# GH Workflows
48+
.github
+3-14
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
const { join } = require("path");
2-
const { readFileSync } = require("fs-extra");
3-
const { NEXT_DIST_DIR } = require("../config");
4-
const getFilePathForRoute = require("./getFilePathForRoute");
1+
const getDataRouteForRoute = require("./getDataRouteForRoute");
52

6-
// TO-DO: make more DRY with other getDataRoute helper
7-
8-
// Get build ID that is used for data routes, e.g. /_next/data/BUILD_ID/...
9-
const fileContents = readFileSync(join(NEXT_DIST_DIR, "BUILD_ID"));
10-
const buildId = fileContents.toString();
11-
12-
// Return the data route for the given route
3+
// Return the data route for the given route with the correct locale
134
const getDataRouteForI18nRoute = (route, locale) => {
14-
const filePath = getFilePathForRoute(route, "json");
15-
16-
return join("/_next", "data", buildId, locale, filePath);
5+
return getDataRouteForRoute(route, locale);
176
};
187

198
module.exports = getDataRouteForI18nRoute;

lib/helpers/getDataRouteForRoute.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ const fileContents = readFileSync(join(NEXT_DIST_DIR, "BUILD_ID"));
88
const buildId = fileContents.toString();
99

1010
// Return the data route for the given route
11-
const getDataRouteForRoute = (route) => {
11+
const getDataRouteForRoute = (route, locale) => {
1212
const filePath = getFilePathForRoute(route, "json");
13+
14+
if (locale) return `/_next/data/${buildId}/${locale}${filePath}`;
1315
return `/_next/data/${buildId}${filePath}`;
1416
};
1517

0 commit comments

Comments
 (0)