Skip to content

Commit 527ba1c

Browse files
authored
Validate that no static redirect routes are present in public/__redirects after any dynamic rule has been declared to avoid over shadowing (#26307)
1 parent 74062bc commit 527ba1c

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

bin/validate-redirects.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ async function main() {
77
let numUrlsWithFragment = 0;
88
let numDuplicateRedirects = 0;
99
let numNonSlashedRedirects = 0;
10+
let seenDynamicRedirects = false;
11+
let numStaticRedirectsAfterDynamicRedirect = 0;
1012

1113
const validEndings = ["/", "*", ".xml", ".md", ".json", ".html", ".pdf"];
1214

@@ -17,6 +19,17 @@ async function main() {
1719

1820
const [from, to] = line.split(" ");
1921

22+
if (from.includes("*")) {
23+
seenDynamicRedirects = true;
24+
}
25+
26+
if (seenDynamicRedirects && !from.includes("*")) {
27+
console.log(
28+
`✘ Found static redirect after dynamic redirect:\n ${from}`,
29+
);
30+
numStaticRedirectsAfterDynamicRedirect++;
31+
}
32+
2033
if (from === to) {
2134
console.log(`✘ Found infinite redirect:\n ${from} -> ${to}`);
2235
numInfiniteRedirects++;
@@ -49,7 +62,8 @@ async function main() {
4962
numInfiniteRedirects ||
5063
numUrlsWithFragment ||
5164
numDuplicateRedirects ||
52-
numNonSlashedRedirects
65+
numNonSlashedRedirects ||
66+
numStaticRedirectsAfterDynamicRedirect
5367
) {
5468
console.log("\nDetected errors:");
5569

@@ -71,6 +85,12 @@ async function main() {
7185
);
7286
}
7387

88+
if (numStaticRedirectsAfterDynamicRedirect > 0) {
89+
console.log(
90+
`- ${numStaticRedirectsAfterDynamicRedirect} static redirect(s) after dynamic redirect(s)`,
91+
);
92+
}
93+
7494
console.log("\nPlease fix the errors above before merging :)");
7595
process.exit(1);
7696
} else {

0 commit comments

Comments
 (0)