Skip to content

Conversation

brophdawg11
Copy link
Contributor

This dedupes a good bit of code and keeps the middleware chain a bit more consistent between document/data requests since you can now inspect and "see" a redirect in both cases and you don't have to be aware of the implementation detail that is the turbo-stream encoding and the 202 response. We just wait until right before we send the response before we serialize it via turbo-stream.

function middleware() {
  let res = await next();

  // Currently, `res` would be a 202 response on `.data` requests and you can't easily 
  // see the location because it's encoded in the body.

  // Now, it's a standard 3xx redirect with a `Location` header on both document 
  // and data requests

  return res;
}

function loader() {
  return redirect('/');
}

Copy link

changeset-bot bot commented Aug 20, 2025

🦋 Changeset detected

Latest commit: 71c1f8c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 11 packages
Name Type
react-router Patch
@react-router/architect Patch
@react-router/cloudflare Patch
@react-router/dev Patch
react-router-dom Patch
@react-router/express Patch
@react-router/node Patch
@react-router/serve Patch
@react-router/fs-routes Patch
@react-router/remix-routes-option-adapter Patch
create-react-router Patch

Not sure what this means? Click here to learn what changesets are.

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

@brophdawg11 brophdawg11 force-pushed the brophdawg11/single-fetch-redirect-serialization branch from 16f464f to 71c1f8c Compare August 20, 2025 18:54
@brophdawg11 brophdawg11 merged commit 4dfb883 into dev Aug 20, 2025
8 checks passed
@brophdawg11 brophdawg11 deleted the brophdawg11/single-fetch-redirect-serialization branch August 20, 2025 19:59
Copy link
Contributor

🤖 Hello there,

We just published version 7.8.2-pre.0 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@MichaelDeBoey MichaelDeBoey changed the title Delay turbo-stream serialization of .data redirects feat(react-router/server-runtime): delay turbo-stream serialization of .data redirects Aug 21, 2025
@MichaelDeBoey MichaelDeBoey changed the title feat(react-router/server-runtime): delay turbo-stream serialization of .data redirects fix(react-router/server-runtime): delay turbo-stream serialization of .data redirects Aug 21, 2025
Copy link
Contributor

🤖 Hello there,

We just published version 7.8.2 which includes this pull request. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@aaronadamsCA
Copy link

@brophdawg11, I think this change may have unintentionally had the opposite effect on our Playwright integration tests.

We have route actions that throw redirects in specific cases. Until now, tests for those cases could just examine a response object's Location header.

As of 7.8.2, it looks like those tests would instead "have to be aware of the implementation detail that is the turbo-stream encoding and the 202 response".

I have no idea what to do with this finding, what do you think? Should I file a bug, a request for test tools, or something else entirely?

@brophdawg11
Copy link
Contributor Author

@aaronadamsCA I think I would need to see an example. This shouldn't change anything about how redirects are returned on document or data requests:

  • document requests should return normal 3xx redirect responses with a Location header
  • data requests should still return 202 responses with the redirect info encoded into the body via turbo-stream

What should change here is the point in time when redirects on .data requests are converted from a 3xx to a 202 when middleware is enabled. You used to get a 202 from await next() on .data requests in middleware, but now you'll get the standard 3xx and it'll be converted to a 202 as the very last step before being returns from the react router handler.

Here's a quick example: https://stackblitz.com/edit/github-5cdfffvj?file=app%2Froutes%2Fredirect.tsx

If you downgrade that to 7.8.1 you can see the old 202 behavior.

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.

4 participants