Skip to content

Commit

Permalink
refactor: deprecate reactRouterRedirect in favor of redirect helper. …
Browse files Browse the repository at this point in the history
…Add getPath helper (#62)
  • Loading branch information
rphlmr authored Jan 14, 2025
1 parent f7bcc19 commit 977848b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,18 +508,20 @@ export interface HonoServerOptions<E extends Env = BlankEnv> extends Omit<HonoSe
🚨 Redirecting from a middleware
> [!IMPORTANT]
> TLDR: If you encounter a `Error: Unable to decode turbo-stream response` after a redirect from your middleware, try to use `reactRouterRedirect` instead of `c.redirect`.
> TLDR: If you encounter a `Error: Unable to decode turbo-stream response` after a redirect from your middleware, try to use `redirect` instead of `c.redirect`.
>
> `redirect` will use `c.redirect` for "normal" requests and a single-fetch-like response for React Router `.data` requests.
>
> If the next handler is a Hono middleware (ex: https://github.com/rphlmr/react-router-hono-server/discussions/56), you can use `c.redirect` as usual.
>
> You **have to** use the `reactRouterRedirect` helper to redirect from a middleware if the next handler that will receive this redirect is a React Router route.
> You **have to** use the `redirect` helper to redirect from a middleware if the next handler that will receive this redirect is a React Router route.
>
> It returns a single-fetch-like response.
>
> If you use `c.redirect`, it will not work as expected and you will get a `Unable to decode turbo-stream response` error.
>
>```ts
> import { reactRouterRedirect } from "react-router-hono-server/http";
> import { redirect } from "react-router-hono-server/http";
>```
>
> I'm sorry for this inconvenience, I hope it can be "fixed" in future React Router versions.
Expand Down
4 changes: 2 additions & 2 deletions examples/node/protected-routes/app/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getConnInfo } from "@hono/node-server/conninfo";
import { createMiddleware } from "hono/factory";
import { reactRouterRedirect } from "react-router-hono-server/http";
import { redirect } from "react-router-hono-server/http";
import { createHonoServer } from "react-router-hono-server/node";

console.log("loading server");
Expand All @@ -17,7 +17,7 @@ const protectRoutes = createMiddleware(async (c, next) => {
console.log("Checking path:", c.req.path);

if (c.req.path.includes("/protected")) {
return reactRouterRedirect("/");
return redirect(c, "/");
}

return next();
Expand Down
26 changes: 26 additions & 0 deletions src/http.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import type { Context } from "hono";

/**
* Redirect to a new location in a way that React Router can handle.
*
* It follows the Single Fetch Redirect protocol.
*
* @deprecated Use `redirect` instead. `import { reactRouterRedirect } from "react-router-hono-server/http"`
*/
export function reactRouterRedirect(location: string) {
return new Response(
Expand Down Expand Up @@ -42,3 +46,25 @@ export function reactRouterRedirect(location: string) {
}
);
}

/**
* Redirect to a new location.
*
* It follows the Single Fetch Redirect protocol, if the request path ends with `.data`.
*/
export function redirect(c: Context, location: string) {
if (c.req.path.includes(".data")) {
return reactRouterRedirect(location);
}

return c.redirect(location);
}

/**
* Get the current request path
*
* If the path ends with `.data` (React Router Single Fetch query), it will be removed.
*/
function getPath(c: Context) {
return c.req.path.replace(".data", "");
}

0 comments on commit 977848b

Please sign in to comment.