fix: replace instanceof checks with duck-typing for NextRequestHint compatibility #2476
+16
−4
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.
📋 Changes
Problem
When server actions trigger token refresh in middleware, the token refresh fails because
NextRequestHintobjects failinstanceof NextRequestchecks in theupdateSession()andsaveToSession()methods.This causes the code to incorrectly fall through to the Pages Router path, which tries to call
appendHeader()- a method that doesn't exist onNextResponse, resulting in runtime errors.Error:
Affected Users
Solution
Replace
instanceof NextRequest && res instanceof NextResponsechecks with duck-typing that validates the presence of required methods:Why Duck-Typing?
Code Changes
File:
src/server/client.ts1.
updateSession()method (~line 936)Before:
After:
2.
saveToSession()method (~line 1148)Before:
After:
Impact
Before
appendHeader is not a functionAfter
Breaking Changes
None. This is a backward-compatible bug fix that maintains all existing functionality.
📎 References
Fixes #2253
Related to #2124, #1934
🎯 Testing
Verified With
Test Scenario
Note on Tests: