-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Restore handling of 204 "soft" redirects on data requests #13364
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
Merged
+105
−10
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eec2ec2
Add a new dataRedirect utility for external redirects on .data reqeusts
brophdawg11 a9bed75
Fix typecheck
brophdawg11 e15116a
fix e2e test
brophdawg11 1eeecd9
Remove dataRedirect utility
brophdawg11 1dbaf2b
Merge branch 'dev' into brophdawg11/single-fetch-redirects
brophdawg11 748bf13
Avoid circular ddependency
brophdawg11 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"react-router": patch | ||
--- | ||
|
||
Update Single Fetch to also handle the 204 redirects used in `?_data` requests in Remix v2 | ||
|
||
- This allows applications to return a redirect on `.data` requests from outside the scope of React Router (i.e., an `express`/`hono` middleware) | ||
- ⚠️ Please note that doing so relies on implementation details that are subject to change without a SemVer major release | ||
- This is primarily done to ease upgrading to Single Fetch for existing Remix v2 applications, but the recommended way to handle this is redirecting from a route middleware |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,13 @@ interface StreamTransferProps { | |
nonce?: string; | ||
} | ||
|
||
// We can't use a 3xx status or else the `fetch()` would follow the redirect. | ||
// We need to communicate the redirect back as data so we can act on it in the | ||
// client side router. We use a 202 to avoid any automatic caching we might | ||
// get from a 200 since a "temporary" redirect should not be cached. This lets | ||
// the user control cache behavior via Cache-Control | ||
export const SINGLE_FETCH_REDIRECT_STATUS = 202; | ||
|
||
// Some status codes are not permitted to have bodies, so we want to just | ||
// treat those as "no data" instead of throwing an exception: | ||
// https://datatracker.ietf.org/doc/html/rfc9110#name-informational-1xx | ||
|
@@ -535,6 +542,22 @@ async function fetchAndDecodeViaTurboStream( | |
throw new ErrorResponseImpl(404, "Not Found", true); | ||
} | ||
|
||
// Handle non-RR redirects (i.e., from express middleware) | ||
if (res.status === 204 && res.headers.has("X-Remix-Redirect")) { | ||
return { | ||
status: SINGLE_FETCH_REDIRECT_STATUS, | ||
data: { | ||
redirect: { | ||
redirect: res.headers.get("X-Remix-Redirect")!, | ||
status: Number(res.headers.get("X-Remix-Status") || "302"), | ||
revalidate: res.headers.get("X-Remix-Revalidate") === "true", | ||
reload: res.headers.get("X-Remix-Reload-Document") === "true", | ||
replace: res.headers.get("X-Remix-Replace") === "true", | ||
}, | ||
}, | ||
}; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we get back a 204 redirect we convert it to a single fetch redirect here for downstream processing |
||
|
||
if (NO_BODY_STATUS_CODES.has(res.status)) { | ||
let routes: { [key: string]: SingleFetchResult } = {}; | ||
// We get back just a single result for action requests - normalize that | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pulled into here from the server-runtime file to avoid circular deps