Replies: 2 comments 6 replies
-
[edit] There's also a workaround by @rphlmr for Hono. https://github.com/rphlmr/react-router-hono-server/blob/506487503c520d79baaf4f92a176dc869ba5472e/src/http.ts#L6 Here's the temporary workaround I used, but react-router should really support this as it requires using and emulating a lot of internals. Checking auth in express/hono middleware and redirecting if not authenticated is a common use case I'd think also. import { encode } from "turbo-stream";
import { UNSAFE_SingleFetchRedirectSymbol } from "react-router";
import { writeReadableStreamToWritable } from "@react-router/node";
const SINGLE_FETCH_REDIRECT_STATUS = 202;
function redirectSingleFetch(url: string, res: Response) {
const readableStream = encode(
{
[UNSAFE_SingleFetchRedirectSymbol]: {
redirect: url,
status: 302,
// Modify these for your use-case
revalidate: false,
reload: true,
replace: false,
},
},
{
plugins: [
(value) => {
if (
value &&
typeof value === "object" &&
UNSAFE_SingleFetchRedirectSymbol in value
) {
return [
"SingleFetchRedirect",
value[UNSAFE_SingleFetchRedirectSymbol],
];
}
},
],
}
);
res.status(SINGLE_FETCH_REDIRECT_STATUS);
return writeReadableStreamToWritable(readableStream, res);
} |
Beta Was this translation helpful? Give feedback.
-
This is currently a blocker for Docker to adopt We need to be able to have hosting server middleware (
To unblock us for now, I'm probably going to patch But for RR7 it would be incredibly helpful to have some sort of "interop" helpers that can be used to create compatible Responses from outside of the React Router handler, especially as we don't currently have middleware support and have to rely on our server's middleware abstractions for many things. |
Beta Was this translation helpful? Give feedback.
-
First of all, I would like to express my gratitude to the members who maintain the system.
I am grateful every day for providing the wonderful framework called React Router.
Details
My suggestion is as the title says. Currently, it is very difficult to create a redirect response from an application other than React Router. This is because, when Single fetch is introduced, responses are generated by encoding with turbo stream. If a redirect response is returned without encoding with turbo stream, an error will occur on the client side, that is, the browser side, where it cannot be decoded.
react-router/packages/react-router/lib/server-runtime/server.ts
Lines 162 to 190 in 6c27453
For example, if you use Hono on the server side of React Router, you can easily create middleware using hono-react-router-adapter.
https://github.com/yusukebe/hono-react-router-adapter
However, as explained earlier, if you return a normal redirect response for a JSON request (a request with
.data
in the URL) outside of React Router, that is, with hono, an error will occur in turbo stream decoding on the React Router client side.It is very difficult to return a redirect response with a server adapter other than Hono or React Router.
Improvement proposal
I have two improvement proposals. I haven't thought about it that deeply, so it may be difficult.
Please consider this.
Beta Was this translation helpful? Give feedback.
All reactions