1
1
const { join } = require ( "path" ) ;
2
2
const { logTitle, logItem } = require ( "../../helpers/logger" ) ;
3
+ const getDataRouteForI18nRoute = require ( "../../helpers/getDataRouteForI18nRoute" ) ;
3
4
const getFilePathForRoute = require ( "../../helpers/getFilePathForRoute" ) ;
5
+ const getFilePathForRouteWithI18n = require ( "../../helpers/getFilePathForRouteWithI18n" ) ;
6
+ const getNextConfig = require ( "../../helpers/getNextConfig" ) ;
4
7
const isRouteWithFallback = require ( "../../helpers/isRouteWithFallback" ) ;
5
8
const setupStaticFileForPage = require ( "../../helpers/setupStaticFileForPage" ) ;
6
9
const setupNetlifyFunctionForPage = require ( "../../helpers/setupNetlifyFunctionForPage" ) ;
@@ -20,17 +23,63 @@ const setup = ({ functionsPath, publishPath }) => {
20
23
pages . forEach ( ( { route, dataRoute, srcRoute } ) => {
21
24
logItem ( route ) ;
22
25
23
- // Copy pre-rendered HTML page
24
- const htmlPath = getFilePathForRoute ( route , "html" ) ;
25
- setupStaticFileForPage ( { inputPath : htmlPath , publishPath } ) ;
26
-
27
- // Copy page's JSON data
28
- const jsonPath = getFilePathForRoute ( route , "json" ) ;
29
- setupStaticFileForPage ( {
30
- inputPath : jsonPath ,
31
- outputPath : dataRoute ,
32
- publishPath,
33
- } ) ;
26
+ const nextConfig = getNextConfig ( ) ;
27
+
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
+ 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
37
+ if ( isNotDynamic ) {
38
+ locales . forEach ( ( locale ) => {
39
+ // Copy pre-rendered HTML page
40
+ const route_ = route === "/" ? "" : route ;
41
+ const htmlPath = getFilePathForRouteWithI18n ( route_ , "html" , locale ) ;
42
+ setupStaticFileForPage ( { inputPath : htmlPath , publishPath } ) ;
43
+
44
+ const jsonPath = getFilePathForRouteWithI18n ( route_ , "json" , locale ) ;
45
+
46
+ // the actual route to the json is under pages/{locale}
47
+ // but the dataRoutes are the same for all locales according to
48
+ // prerender-manifest..
49
+ const realJsonDataRoute = getDataRouteForI18nRoute ( route , locale ) ;
50
+ setupStaticFileForPage ( {
51
+ inputPath : jsonPath ,
52
+ outputPath : realJsonDataRoute ,
53
+ publishPath,
54
+ } ) ;
55
+ } ) ;
56
+ } else {
57
+ // Copy pre-rendered HTML page
58
+ const htmlPath = getFilePathForRoute ( route , "html" ) ;
59
+ setupStaticFileForPage ( { inputPath : htmlPath , publishPath } ) ;
60
+
61
+ // Copy page's JSON data
62
+ const jsonPath = getFilePathForRoute ( route , "json" ) ;
63
+ setupStaticFileForPage ( {
64
+ inputPath : jsonPath ,
65
+ outputPath : dataRoute ,
66
+ publishPath,
67
+ } ) ;
68
+ }
69
+ // TO-DO combine these conditions
70
+ } else {
71
+ // Copy pre-rendered HTML page
72
+ const htmlPath = getFilePathForRoute ( route , "html" ) ;
73
+ setupStaticFileForPage ( { inputPath : htmlPath , publishPath } ) ;
74
+
75
+ // Copy page's JSON data
76
+ const jsonPath = getFilePathForRoute ( route , "json" ) ;
77
+ setupStaticFileForPage ( {
78
+ inputPath : jsonPath ,
79
+ outputPath : dataRoute ,
80
+ publishPath,
81
+ } ) ;
82
+ }
34
83
35
84
// // Set up the Netlify function (this is ONLY for preview mode)
36
85
const relativePath = getFilePathForRoute ( srcRoute || route , "js" ) ;
0 commit comments