-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Add a new dataRedirect utility for external 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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"react-router": minor | ||
--- | ||
|
||
Add a new `dataRedirect` utility for performing redirects on `.data` requests outside of the React Router handler |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ import { escapeHtml } from "./markup"; | |
import type { RouteModule, RouteModules } from "./routeModules"; | ||
import invariant from "./invariant"; | ||
import type { EntryRoute } from "./routes"; | ||
import { SINGLE_FETCH_REDIRECT_STATUS } from "../../server-runtime/single-fetch"; | ||
|
||
export const SingleFetchRedirectSymbol = Symbol("SingleFetchRedirect"); | ||
|
||
|
@@ -550,6 +551,25 @@ async function fetchAndDecode( | |
throw new ErrorResponseImpl(404, "Not Found", true); | ||
} | ||
|
||
// Handle non-RR redirects (i.e., from express middleware via `dataRedirect`) | ||
if (res.status === 204 && res.headers.has("X-Remix-Redirect")) { | ||
let data: SingleFetchRedirectResult = { | ||
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", | ||
}; | ||
if (!init.method || init.method === "GET") { | ||
return { | ||
status: SINGLE_FETCH_REDIRECT_STATUS, | ||
data: { [SingleFetchRedirectSymbol]: data }, | ||
}; | ||
} else { | ||
return { status: SINGLE_FETCH_REDIRECT_STATUS, data }; | ||
} | ||
} | ||
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 |
||
|
||
// some status codes are not permitted to have bodies, so we want to just | ||
// treat those as "no data" instead of throwing an exception. | ||
// 304 is not included here because the browser should fill those responses | ||
|
@@ -657,13 +677,13 @@ function unwrapSingleFetchResult(result: SingleFetchResult, routeId: string) { | |
} else if ("redirect" in result) { | ||
let headers: Record<string, string> = {}; | ||
if (result.revalidate) { | ||
headers["X-Remix-Revalidate"] = "yes"; | ||
headers["X-Remix-Revalidate"] = "true"; | ||
} | ||
if (result.reload) { | ||
headers["X-Remix-Reload-Document"] = "yes"; | ||
headers["X-Remix-Reload-Document"] = "true"; | ||
} | ||
if (result.replace) { | ||
headers["X-Remix-Replace"] = "yes"; | ||
headers["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. Align these with values used elsewhere - value doesn't really matter since we use |
||
} | ||
throw redirect(result.redirect, { status: result.status, headers }); | ||
} else if ("data" in result) { | ||
|
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.
We don't export our internal logic for converting a fetch
Response
to an expressres
so for now it's assumed that the application has to do that adapter logic on their own. I think this is ok since status/headers are all that matter? There isn't a body on these responses