@@ -2,6 +2,8 @@ const { join } = require("path");
22const { logTitle, logItem } = require ( "../../helpers/logger" ) ;
33const { NETLIFY_PUBLISH_PATH } = require ( "../../config" ) ;
44const getFilePathForRoute = require ( "../../helpers/getFilePathForRoute" ) ;
5+ const getFilePathForRouteWithI18n = require ( "../../helpers/getFilePathForRouteWithI18n" ) ;
6+ const getNextConfig = require ( "../../helpers/getNextConfig" ) ;
57const isRouteWithFallback = require ( "../../helpers/isRouteWithFallback" ) ;
68const setupStaticFileForPage = require ( "../../helpers/setupStaticFileForPage" ) ;
79const setupNetlifyFunctionForPage = require ( "../../helpers/setupNetlifyFunctionForPage" ) ;
@@ -21,13 +23,56 @@ const setup = () => {
2123 pages . forEach ( ( { route, dataRoute, srcRoute } ) => {
2224 logItem ( route ) ;
2325
24- // Copy pre-rendered HTML page
25- const htmlPath = getFilePathForRoute ( route , "html" ) ;
26- setupStaticFileForPage ( htmlPath ) ;
26+ const nextConfig = getNextConfig ( ) ;
2727
28- // Copy page's JSON data
29- const jsonPath = getFilePathForRoute ( route , "json" ) ;
30- setupStaticFileForPage ( jsonPath , dataRoute ) ;
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+ // ok so you dont need to loop over for srcRoutes and do the
35+ // set up twice
36+ const isNotDynamic = ! srcRoute ;
37+ // Dynamic routes dont need special helper, Next auto prepends with locale
38+ // in prerender-manifest
39+ if ( isNotDynamic ) {
40+ locales . forEach ( ( locale ) => {
41+ // Copy pre-rendered HTML page
42+ const htmlPath = getFilePathForRouteWithI18n ( route , "html" , locale ) ;
43+ setupStaticFileForPage ( htmlPath ) ;
44+ } ) ;
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 ) ;
58+ } else {
59+ // Copy pre-rendered HTML page
60+ const htmlPath = getFilePathForRoute ( route , "html" ) ;
61+ setupStaticFileForPage ( htmlPath ) ;
62+
63+ // Copy page's JSON data
64+ const jsonPath = getFilePathForRoute ( route , "json" ) ;
65+ setupStaticFileForPage ( jsonPath , dataRoute ) ;
66+ }
67+ } else {
68+ // Copy pre-rendered HTML page
69+ const htmlPath = getFilePathForRoute ( route , "html" ) ;
70+ setupStaticFileForPage ( htmlPath ) ;
71+
72+ // Copy page's JSON data
73+ const jsonPath = getFilePathForRoute ( route , "json" ) ;
74+ setupStaticFileForPage ( jsonPath , dataRoute ) ;
75+ }
3176
3277 // // Set up the Netlify function (this is ONLY for preview mode)
3378 const relativePath = getFilePathForRoute ( srcRoute || route , "js" ) ;
0 commit comments