Skip to content

Commit

Permalink
Cast redirectBack return value to TypedResponse<never> (sergiodxa#303)
Browse files Browse the repository at this point in the history
## Context
ATM, the current return type of `redirectBack` is `Response`, making
type inference messy when doing :
```ts
const fetcher = useFetcher<typeof action>();
```
I changed the return type based on the one of `redirect` from Remix code

## Example : 
With my current code base were I spotted the issue

**Before**
<img width="771" alt="image"
src="https://github.com/sergiodxa/remix-utils/assets/40292402/7963f707-9e59-4b28-b850-d9a17aee8d52">

**After**
<img width="767" alt="image"
src="https://github.com/sergiodxa/remix-utils/assets/40292402/f4641a6e-eb29-43d0-9492-8d1995f9c0e4">

This happen when you mix `redirectBack` with other return statements,
like the following
```ts
export async function action({ request }: ActionFunctionArgs) {
  const formData = await request.formData();
  const submission = parse(formData);

  if (!submission.isValid) {
    return json(submission);
  }

 return redirectBack(request, {
      fallback: getRoute('/scenarios/:scenarioId', {
        scenarioId: fromUUID(submission.value.scenarioId),
      }),
    });
}
```
  • Loading branch information
balzdur authored Feb 23, 2024
1 parent 8d83bcd commit b9ac836
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/server/redirect-back.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TypedResponse } from "@remix-run/node";

/**
* Create a new Response with a redirect set to the URL the user was before.
* It uses the Referer header to detect the previous URL. It asks for a fallback
Expand All @@ -15,7 +17,7 @@
export function redirectBack(
request: Request,
{ fallback, ...init }: ResponseInit & { fallback: string },
): Response {
): TypedResponse<never> {
let responseInit = init;
if (typeof responseInit === "number") {
responseInit = { status: responseInit };
Expand All @@ -29,5 +31,5 @@ export function redirectBack(
return new Response(null, {
...responseInit,
headers,
});
}) as TypedResponse<never>;
}

0 comments on commit b9ac836

Please sign in to comment.