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

Commit e9172ad

Browse files
committed
Force redirects with conditions before plain redirects
1 parent cce9b57 commit e9172ad

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

lib/steps/setupRedirects.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ const setupRedirects = (publishPath) => {
5252
// Sort routes: More-specific routes precede less-specific routes (e.g., catch-all)
5353
const getSortedRedirects = (redirects) => {
5454
const sortedRoutes = getSortedRoutes(redirects.map(({ route }) => route));
55-
return redirects.sort(
56-
(a, b) => sortedRoutes.indexOf(a.route) - sortedRoutes.indexOf(b.route)
57-
);
55+
return redirects.sort((a, b) => {
56+
// If routes are different, sort according to Next.js' getSortedRoutes
57+
if (a.route !== b.route)
58+
return sortedRoutes.indexOf(a.route) - sortedRoutes.indexOf(b.route);
59+
60+
// Otherwise, put the route with more conditions first
61+
return (b.conditions || []).length - (a.conditions || []).length;
62+
});
5863
};
5964
const sortedStaticRedirects = getSortedRedirects(staticRedirects);
6065
const sortedDynamicRedirects = getSortedRedirects(dynamicRedirects);

0 commit comments

Comments
 (0)