Proposal: Allow NextResponse instance to perform redirect() and rewrite() operations #82195
mattiamalonni
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Goals
NextResponse
instances (e.g., redirecting) without needing to re-instantiate a new response object.response
variable.Non-Goals
NextResponse.redirect()
or.rewrite()
currently work as static methods.Background
In current middleware implementations, every time a redirect or rewrite is required, developers must create a new instance of
NextResponse
using static methods likeNextResponse.redirect(...)
. This leads to a pattern where theresponse
variable must be reassigned multiple times, making the code less readable and harder to manage when handling side effects or cleanup (e.g., clearing cookies or sessions) before returning.For example:
This kind of reassignment breaks the flow and can introduce bugs or inconsistencies, especially when side effects rely on a shared response instance. No current workaround exists to fluently mutate the same response object.
Proposal
Add instance methods to
NextResponse
to support redirecting, rewriting, and other mutations fluently. For example:Beta Was this translation helpful? Give feedback.
All reactions