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

Commit a5ef29a

Browse files
committed
update for dataRoutes
1 parent 5f34aa1 commit a5ef29a

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { join } = require("path");
2+
const { readFileSync } = require("fs-extra");
3+
const { NEXT_DIST_DIR } = require("../config");
4+
const getFilePathForRoute = require("./getFilePathForRoute");
5+
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
13+
const getDataRouteForI18nRoute = (route, locale) => {
14+
const filePath = getFilePathForRoute(route, "json");
15+
16+
return join("/_next", "data", buildId, locale, filePath);
17+
};
18+
19+
module.exports = getDataRouteForI18nRoute;

lib/pages/getStaticProps/setup.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const { join } = require("path");
22
const { logTitle, logItem } = require("../../helpers/logger");
33
const { NETLIFY_PUBLISH_PATH } = require("../../config");
4+
const getDataRouteForI18nRoute = require("../../helpers/getDataRouteForI18nRoute");
45
const getFilePathForRoute = require("../../helpers/getFilePathForRoute");
56
const getFilePathForRouteWithI18n = require("../../helpers/getFilePathForRouteWithI18n");
67
const getNextConfig = require("../../helpers/getNextConfig");
@@ -31,30 +32,24 @@ const setup = () => {
3132
const { locales } = nextConfig.i18n;
3233
if (!locales || locales.length === 0) return;
3334

34-
// ok so you dont need to loop over for srcRoutes and do the
35-
// set up twice
3635
const isNotDynamic = !srcRoute;
37-
// Dynamic routes dont need special helper, Next auto prepends with locale
38-
// in prerender-manifest
36+
// Dynamic routes don't need special helper, Next auto prepends with locale
37+
// in prerender-manifest, but static routes are missing locale
3938
if (isNotDynamic) {
4039
locales.forEach((locale) => {
4140
// Copy pre-rendered HTML page
42-
const htmlPath = getFilePathForRouteWithI18n(route, "html", locale);
41+
const route_ = route === "/" ? "" : route;
42+
const htmlPath = getFilePathForRouteWithI18n(route_, "html", locale);
4343
setupStaticFileForPage(htmlPath);
44+
45+
const jsonPath = getFilePathForRouteWithI18n(route_, "json", locale);
46+
47+
// the actual route to the json is under pages/{locale}
48+
// but the dataRoutes are the same for all locales according to
49+
// prerender-manifest..
50+
const realJsonDataRoute = getDataRouteForI18nRoute(route, locale);
51+
setupStaticFileForPage(jsonPath, realJsonDataRoute);
4452
});
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);
5853
} else {
5954
// Copy pre-rendered HTML page
6055
const htmlPath = getFilePathForRoute(route, "html");
@@ -64,6 +59,7 @@ const setup = () => {
6459
const jsonPath = getFilePathForRoute(route, "json");
6560
setupStaticFileForPage(jsonPath, dataRoute);
6661
}
62+
// TO-DO combine these conditions
6763
} else {
6864
// Copy pre-rendered HTML page
6965
const htmlPath = getFilePathForRoute(route, "html");

0 commit comments

Comments
 (0)