Add ESLint Rule to Prevent Redirect in Try-Catch Block #68108
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.
What?
This PR introduces a new ESLint rule to prevent the use of
redirect
within a try-catch block in Next.js applications.Why?
This change was inspired by a discussion on X here. Using
redirect
within a try-catch block can prevent redirects from executing correctly becauseredirect
throws aNEXT_REDIRECT
error internally, which can be caught and handled by the catch block, thus stopping the redirect.How?
ESLint Plugin:
index.ts
to register theno-redirect-in-try-catch
rule.no-redirect-in-try-catch.ts
to define the rule logic.Testing:
no-redirect-in-try-catch.test.ts
to validate correct and incorrect usage of theredirect
function within try-catch blocks.Documentation Update:
02-eslint.mdx
to include the new rule.no-redirect-in-try-catch.mdx
to explain the new rule, why it is necessary, and how to fix violations.