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

Support for i18n in Next 10 #75

Merged
merged 22 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
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 Nov 12, 2020
89b22e6
update withoutProps/redirects to accommodate defaultLocale
lindsaylevine Nov 29, 2020
7da18c2
very dirty Just Working i18n for most cases
lindsaylevine Dec 11, 2020
b475f4d
pre clean-up tests, no cypress yet
lindsaylevine Dec 11, 2020
c68d361
make i18n getDataRoute helper more DRY
lindsaylevine Dec 11, 2020
0c67c4b
fix windows test
lindsaylevine Dec 11, 2020
0498d83
add documentation for withoutProps and SSR pages logic for i18n
lindsaylevine Dec 12, 2020
c86eb09
clean up getStaticProps setup logic
lindsaylevine Dec 12, 2020
348705d
fix getStaticProps redirects logic to be consistent with setup and he…
lindsaylevine Dec 12, 2020
d45f240
fix revalidate redirects logic and heavily comment
lindsaylevine Dec 12, 2020
9609739
remove superfluous getDataRouteForI18nRoute helper
lindsaylevine Dec 12, 2020
fb11d14
remove superfluous getFilePathForRouteWithI18n helper
lindsaylevine Dec 12, 2020
90856ad
fix existing cypress tests except a few v odd cases
lindsaylevine Dec 16, 2020
acc13c9
fix/move fallback i18n logic
lindsaylevine Dec 17, 2020
db05c51
fix previously commented out failing dataRoute tests
lindsaylevine Dec 19, 2020
2d5e867
fix 404 cypress tests which expect root level 404.html
lindsaylevine Dec 19, 2020
04d57b4
specifically test i18n/non-default locales in cypress
lindsaylevine Dec 20, 2020
60e8a49
root level index pages need special dataRoute logic
lindsaylevine Dec 20, 2020
0d45036
use splats for dynamic ssg i18n dataRoutes
lindsaylevine Dec 20, 2020
96959bd
plain static routes need 2 redirects: one to defaultLocale, one to pr…
lindsaylevine Dec 20, 2020
447426d
massive cleanup/refactor to transform manifest
lindsaylevine Dec 22, 2020
25ff5fe
missing initialProps + fallback logic + PR feedback
lindsaylevine Jan 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/pages/getStaticProps/redirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@ pages.forEach(({ route, dataRoute, srcRoute }) => {
const { defaultLocale, locales = [] } = nextConfig.i18n;
const isDynamic = !!srcRoute;
if (!isDynamic) {
// Preview mode redirect for standard route (no locale)
redirects.push({
route,
target,
force: false, // force FALSE to allow collision with defaultLocale redirect below
conditions,
});
// Add a redirect for "expected" route to the defaultLocale-prepended route
// i.e. /getStaticProps -> /en/getStaticProps
if (defaultLocale) {
redirects.push({
route,
target: `/${defaultLocale}${route}`,
});
}
redirects.push({
route,
target: `/${defaultLocale}${route}`,
});
// Add a preview mode redirect for every locale
locales.forEach((locale) => {
const pageRoute = `/${locale}${route}`;
Expand Down Expand Up @@ -82,6 +87,12 @@ pages.forEach(({ route, dataRoute, srcRoute }) => {
!dynamicDefaultLocaleRedirectsAdded.includes(srcRoute) &&
!isFallbackRoute(srcRoute)
) {
redirects.push({
route: srcRoute,
target,
force: false, // force FALSE to allow collision with defaultLocale redirect below
conditions,
});
const formattedSrcRoute = srcRoute.replace("[", ":").replace("]", "");
const defaultLocaleTarget = `/${defaultLocale}${formattedSrcRoute}`;
redirects.push({
Expand Down
12 changes: 10 additions & 2 deletions lib/steps/setupRedirects.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ const setupRedirects = (publishPath) => {
...require("../pages/withoutProps/redirects"),
];

// Enables us to add colliding route redirects (which may exist
// for good reason i.e. i18n, you have two redirects; one for
// /route /{defaultLocale/route & /route -> preview_mode_func)
const redirectsTracked = nextRedirects.map((redirect) => {
return { ...redirect, added: false };
});

// Add _redirect section heading
if (nextRedirects.length >= 1) redirects.push("# Next-on-Netlify Redirects");

Expand All @@ -38,8 +45,8 @@ const setupRedirects = (publishPath) => {

// Assemble redirects for each route
sortedRoutes.forEach((route) => {
const nextRedirect = nextRedirects.find(
(redirect) => redirect.route === route
const nextRedirect = redirectsTracked.find(
(redirect) => redirect.route === route && !redirect.added
);

// One route may map to multiple Netlify routes: e.g., catch-all pages
Expand All @@ -60,6 +67,7 @@ const setupRedirects = (publishPath) => {
const redirect = redirectPieces.join(" ").trim();
logItem(redirect);
redirects.push(redirect);
nextRedirect.added = true;
});
});

Expand Down
2 changes: 2 additions & 0 deletions tests/__snapshots__/i18n.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ exports[`Routing creates Netlify redirects 1`] = `
/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
/getStaticProps/static /.netlify/functions/next_getStaticProps_static 200 Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/static /en/getStaticProps/static 200
/getStaticProps/with-revalidate /.netlify/functions/next_getStaticProps_withrevalidate 200
/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
/getStaticProps/withRevalidate/:id /.netlify/functions/next_getStaticProps_withRevalidate_id 200
/getStaticProps/:id /.netlify/functions/next_getStaticProps_id 200 Cookie=__prerender_bypass,__next_preview_data
/getStaticProps/:id /en/getStaticProps/:id 200
/shows/:id /.netlify/functions/next_shows_id 200
/shows/:params/* /.netlify/functions/next_shows_params 200
Expand Down