Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ESLint Rule to Prevent Redirect in Try-Catch Block #68108

Open
wants to merge 8 commits into
base: canary
Choose a base branch
from

Conversation

piotrski
Copy link

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 because redirect throws a NEXT_REDIRECT error internally, which can be caught and handled by the catch block, thus stopping the redirect.

How?

ESLint Plugin:

  • Modified index.ts to register the no-redirect-in-try-catch rule.
  • Created no-redirect-in-try-catch.ts to define the rule logic.

Testing:

  • Created no-redirect-in-try-catch.test.ts to validate correct and incorrect usage of the redirect function within try-catch blocks.

Documentation Update:

  • Updated 02-eslint.mdx to include the new rule.
  • Created no-redirect-in-try-catch.mdx to explain the new rule, why it is necessary, and how to fix violations.

@ijjk ijjk added Documentation Related to Next.js' official documentation. tests type: next labels Jul 24, 2024
@ijjk
Copy link
Member

ijjk commented Jul 24, 2024

Allow CI Workflow Run

  • approve CI run for commit: 6362a6f

Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer

Copy link
Contributor

@AhmedBaset AhmedBaset left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, here's a more natural and realistic version:

  1. The rule shouldn't just be limited to redirect errors. It should cover all Next.js thrown errors, including redirect, notFound, req.nextUrl.searchParams, and props.searchParams.
  2. Rather than blocking these functions, it should make sure to call unstable_rethrow at the beginning of the catch block.

@piotrski
Copy link
Author

piotrski commented Aug 7, 2024

@AhmedBaset I appreciate your suggestion regarding the scope of the rule. I can add rules for redirect and notFound in this PR, but they should probably be separate rules to keep with the convention. As for req.nextUrl.searchParams and props.searchParams, it might be better to address those in a separate PR. What do you think about this approach?

Regarding the use of unstable_rethrow, I'm not entirely sure since the latest documentation for actions recommends using useActionState for error handling. You can find more details here: Next.js Error Handling Documentation.

@feedthejim
Copy link
Contributor

great work @piotrski, could you update the rule to suggest the usage of rethrow as specified in this RFC #64076 ?

@piotrski
Copy link
Author

piotrski commented Sep 4, 2024

@feedthejim, apologies for the delay, and thanks for your feedback!

I’ve updated the rule to enforce unstable_rethrow when using redirect inside a try-catch, as per RFC #64076. The rule has been renamed to no-redirect-in-try-catch-without-rethrow, and the documentation has been updated with new examples.

Quick question: should we handle notFound() and permanentRedirect() the same way since they also throw errors internally, or would it be better to create separate rules for them?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Related to Next.js' official documentation. tests type: next
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants