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

Commit 96959bd

Browse files
committed
plain static routes need 2 redirects: one to defaultLocale, one to preview mode func
1 parent 0d45036 commit 96959bd

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

lib/pages/getStaticProps/redirects.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,19 @@ pages.forEach(({ route, dataRoute, srcRoute }) => {
4545
const { defaultLocale, locales = [] } = nextConfig.i18n;
4646
const isDynamic = !!srcRoute;
4747
if (!isDynamic) {
48+
// Preview mode redirect for standard route (no locale)
49+
redirects.push({
50+
route,
51+
target,
52+
force: false, // force FALSE to allow collision with defaultLocale redirect below
53+
conditions,
54+
});
4855
// Add a redirect for "expected" route to the defaultLocale-prepended route
4956
// i.e. /getStaticProps -> /en/getStaticProps
50-
if (defaultLocale) {
51-
redirects.push({
52-
route,
53-
target: `/${defaultLocale}${route}`,
54-
});
55-
}
57+
redirects.push({
58+
route,
59+
target: `/${defaultLocale}${route}`,
60+
});
5661
// Add a preview mode redirect for every locale
5762
locales.forEach((locale) => {
5863
const pageRoute = `/${locale}${route}`;
@@ -82,6 +87,12 @@ pages.forEach(({ route, dataRoute, srcRoute }) => {
8287
!dynamicDefaultLocaleRedirectsAdded.includes(srcRoute) &&
8388
!isFallbackRoute(srcRoute)
8489
) {
90+
redirects.push({
91+
route: srcRoute,
92+
target,
93+
force: false, // force FALSE to allow collision with defaultLocale redirect below
94+
conditions,
95+
});
8596
const formattedSrcRoute = srcRoute.replace("[", ":").replace("]", "");
8697
const defaultLocaleTarget = `/${defaultLocale}${formattedSrcRoute}`;
8798
redirects.push({

lib/steps/setupRedirects.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ const setupRedirects = (publishPath) => {
2929
...require("../pages/withoutProps/redirects"),
3030
];
3131

32+
// Enables us to add colliding route redirects (which may exist
33+
// for good reason i.e. i18n, you have two redirects; one for
34+
// /route /{defaultLocale/route & /route -> preview_mode_func)
35+
const redirectsTracked = nextRedirects.map((redirect) => {
36+
return { ...redirect, added: false };
37+
});
38+
3239
// Add _redirect section heading
3340
if (nextRedirects.length >= 1) redirects.push("# Next-on-Netlify Redirects");
3441

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

3946
// Assemble redirects for each route
4047
sortedRoutes.forEach((route) => {
41-
const nextRedirect = nextRedirects.find(
42-
(redirect) => redirect.route === route
48+
const nextRedirect = redirectsTracked.find(
49+
(redirect) => redirect.route === route && !redirect.added
4350
);
4451

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

tests/__snapshots__/i18n.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ exports[`Routing creates Netlify redirects 1`] = `
5050
/getServerSideProps/all/* /.netlify/functions/next_getServerSideProps_all_slug 200
5151
/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
5252
/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200
53+
/getStaticProps/static /.netlify/functions/next_getStaticProps_static 200 Cookie=__prerender_bypass,__next_preview_data
5354
/getStaticProps/static /en/getStaticProps/static 200
5455
/getStaticProps/with-revalidate /.netlify/functions/next_getStaticProps_withrevalidate 200
5556
/getStaticProps/withFallback/:id /.netlify/functions/next_getStaticProps_withFallback_id 200
5657
/getStaticProps/withFallback/:slug/* /.netlify/functions/next_getStaticProps_withFallback_slug 200
5758
/getStaticProps/withRevalidate/withFallback/:id /.netlify/functions/next_getStaticProps_withRevalidate_withFallback_id 200
5859
/getStaticProps/withRevalidate/:id /.netlify/functions/next_getStaticProps_withRevalidate_id 200
60+
/getStaticProps/:id /.netlify/functions/next_getStaticProps_id 200 Cookie=__prerender_bypass,__next_preview_data
5961
/getStaticProps/:id /en/getStaticProps/:id 200
6062
/shows/:id /.netlify/functions/next_shows_id 200
6163
/shows/:params/* /.netlify/functions/next_shows_params 200

0 commit comments

Comments
 (0)