This repository was archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Support for i18n in Next 10 #75
Merged
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
cc4de30
rough pass at i18n ssg breaking changes in next 10
lindsaylevine 89b22e6
update withoutProps/redirects to accommodate defaultLocale
lindsaylevine 7da18c2
very dirty Just Working i18n for most cases
lindsaylevine b475f4d
pre clean-up tests, no cypress yet
lindsaylevine c68d361
make i18n getDataRoute helper more DRY
lindsaylevine 0c67c4b
fix windows test
lindsaylevine 0498d83
add documentation for withoutProps and SSR pages logic for i18n
lindsaylevine c86eb09
clean up getStaticProps setup logic
lindsaylevine 348705d
fix getStaticProps redirects logic to be consistent with setup and he…
lindsaylevine d45f240
fix revalidate redirects logic and heavily comment
lindsaylevine 9609739
remove superfluous getDataRouteForI18nRoute helper
lindsaylevine fb11d14
remove superfluous getFilePathForRouteWithI18n helper
lindsaylevine 90856ad
fix existing cypress tests except a few v odd cases
lindsaylevine acc13c9
fix/move fallback i18n logic
lindsaylevine db05c51
fix previously commented out failing dataRoute tests
lindsaylevine 2d5e867
fix 404 cypress tests which expect root level 404.html
lindsaylevine 04d57b4
specifically test i18n/non-default locales in cypress
lindsaylevine 60e8a49
root level index pages need special dataRoute logic
lindsaylevine 0d45036
use splats for dynamic ssg i18n dataRoutes
lindsaylevine 96959bd
plain static routes need 2 redirects: one to defaultLocale, one to pr…
lindsaylevine 447426d
massive cleanup/refactor to transform manifest
lindsaylevine 25ff5fe
missing initialProps + fallback logic + PR feedback
lindsaylevine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const i18n = require("./getI18n")(); | ||
const { defaultLocale } = i18n; | ||
|
||
// In i18n projects, we need to create redirects from the "naked" route | ||
// to the defaultLocale-prepended route i.e. /static -> /en/static | ||
// Note: there can only one defaultLocale, but we put it in an array to simplify | ||
// logic in redirects.js files via concatenation | ||
const getDefaultLocaleRedirectForI18n = (route, srcRoute, target) => { | ||
// If no i18n, skip | ||
if (!defaultLocale) return []; | ||
|
||
const routePieces = route.split("/"); | ||
const routeLocale = routePieces[1]; | ||
if (routeLocale === defaultLocale) { | ||
const nakedRoute = | ||
route === `/${routeLocale}` ? "/" : route.replace(`/${routeLocale}`, ""); | ||
// const nakedRoute = routePieces.slice(2, routePieces.length).join("/"); | ||
return [ | ||
{ | ||
route: nakedRoute, | ||
target: target || route, | ||
}, | ||
]; | ||
} | ||
|
||
return []; | ||
}; | ||
|
||
module.exports = getDefaultLocaleRedirectForI18n; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Get the i1i8n details specified in next.config.js, if any | ||
const getNextConfig = require("./getNextConfig"); | ||
|
||
const getI18n = () => { | ||
const nextConfig = getNextConfig(); | ||
|
||
return nextConfig.i18n || { locales: [] }; | ||
}; | ||
|
||
module.exports = getI18n; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const i18n = require("./getI18n")(); | ||
const getDataRouteForRoute = require("./getDataRouteForRoute"); | ||
|
||
const getI18nDataRoute = (route, locale) => { | ||
return route === "/" | ||
? getDataRouteForRoute(`/${locale}`) | ||
: getDataRouteForRoute(route, locale); | ||
}; | ||
|
||
// In i18n projects, Next does not prepend static routes with locales | ||
// so we have to do it manually | ||
const getLocaleRoutesForI18n = ({ route, srcRoute }) => { | ||
// If no i18n or is dynamic route, skip | ||
if (!i18n.defaultLocale || !!srcRoute) return []; | ||
return i18n.locales.map((locale) => { | ||
return { | ||
route: `/${locale}${route}`, | ||
dataRoute: getI18nDataRoute(route, locale), | ||
nakedRoute: route, | ||
}; | ||
}); | ||
}; | ||
|
||
module.exports = getLocaleRoutesForI18n; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,46 @@ | ||
const { join } = require("path"); | ||
const { readJSONSync } = require("fs-extra"); | ||
const { NEXT_DIST_DIR } = require("../config"); | ||
const nextConfig = require("./getNextConfig")(); | ||
const getDataRouteForRoute = require("./getDataRouteForRoute"); | ||
|
||
const transformManifestForI18n = (manifest) => { | ||
const { routes, dynamicRoutes } = manifest; | ||
lindsaylevine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const { defaultLocale, locales } = nextConfig.i18n; | ||
const newRoutes = {}; | ||
Object.entries(routes).forEach( | ||
([route, { dataRoute, initialRevalidateSeconds, srcRoute }]) => { | ||
const isDynamicRoute = !!srcRoute; | ||
if (isDynamicRoute) { | ||
newRoutes[route] = routes[route]; | ||
} else { | ||
locales.forEach((locale) => { | ||
const routeWithPrependedLocale = `/${locale}${route}`; | ||
newRoutes[routeWithPrependedLocale] = { | ||
dataRoute: getDataRouteForRoute(route, locale), | ||
srcRoute: route, | ||
initialRevalidateSeconds: false, | ||
lindsaylevine marked this conversation as resolved.
Show resolved
Hide resolved
lindsaylevine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
}); | ||
} | ||
} | ||
); | ||
const newDynamicRoutes = {}; | ||
Object.entries(dynamicRoutes).forEach(([route, { dataRoute, fallback }]) => { | ||
newDynamicRoutes[route] = { | ||
route, | ||
dataRoute: getDataRouteForRoute(route, defaultLocale), | ||
lindsaylevine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fallback, | ||
}; | ||
}); | ||
lindsaylevine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return { ...manifest, routes: newRoutes, dynamicRoutes: newDynamicRoutes }; | ||
}; | ||
|
||
const getPrerenderManifest = () => { | ||
return readJSONSync(join(NEXT_DIST_DIR, "prerender-manifest.json")); | ||
const manifest = readJSONSync(join(NEXT_DIST_DIR, "prerender-manifest.json")); | ||
if (nextConfig.i18n) return transformManifestForI18n(manifest); | ||
return manifest; | ||
}; | ||
|
||
module.exports = getPrerenderManifest; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rn this is only used by gsp/pages.js, might be able to do without this or rename it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nvm its not used anywhere LuL