Skip to content

fix(react-router): Remove Content-Length Header from Single Fetch Responses to Prevent Errors #13658

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

rururux
Copy link

@rururux rururux commented May 22, 2025

fixes: #12850

While investigating the cause of this issue, I discovered an interesting behavior: removing the Content-Length header from the response results in the application functioning correctly.

Upon further examination, I found the following description in the documentation for Node.js's http module:

Set Content-Length header to limit the response body size.

In fact, when setting a clearly insufficient value such as Content-Length: 6, the response body appears to be affected accordingly.

スクリーンショット 2025-05-22 200127

スクリーンショット 2025-05-22 200153

As described in the issue, this becomes problematic when using response headers intended for redirect responses—such as those generated by authentication libraries—which often do not include a response body.
After considering how best to address this, I concluded that, regardless of whether the response is a redirect, the use of turbo-stream encoding can alter the expected body. Therefore, the most robust solution would be to remove the Content-Length header from all Single Fetch responses, which is what this PR implements.

Additional Notes:
There is a comment on the issue stating that the bug occurred in the development environment but not in production(node20, SSR enabled) . I have yet to determine the reason for this discrepancy.
Nevertheless, since I was able to reliably reproduce and fix the bug, I am submitting this pull request.

Copy link

changeset-bot bot commented May 22, 2025

⚠️ No Changeset found

Latest commit: 1e74116

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@remorses
Copy link

you should remove the content length header in the redirect function instead

@rururux
Copy link
Author

rururux commented May 22, 2025

You're right that the issue tends to be most noticeable when using the redirect function.
However, the problem can occur in other cases as well, as long as Single Fetch responses are involved.
スクリーンショット 2025-05-22 205108

This is because developers typically expect the Content-Length header to reflect the size of the value they explicitly set as the response body.
But in reality, what actually gets sent is a value that has been encoded by turbo-stream, which may differ significantly from the original.
As a result, even responses that aren't redirects can end up with a mismatched Content-Length header, leading to the same kind of truncation issues.

That's why I chose to remove the Content-Length header inside the generateSingleFetchResponse function, which handles all Single Fetch responses, not just redirects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants