Skip to content

Commit

Permalink
Merge pull request #68 from janglad/bug/next-redirect
Browse files Browse the repository at this point in the history
Fix client error when redirecting in action
  • Loading branch information
janglad authored Oct 3, 2024
2 parents 66e261d + daf1a07 commit 407e0aa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/brown-hairs-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"safe-fn-react": patch
---

- Fix possible client side type error when redirecting in server action
8 changes: 7 additions & 1 deletion packages/safe-fn-react/src/useServerAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ export const useServerAction = <TAction extends AnySafeFnAction>(
void ResultAsync.fromThrowable(callbacks.onStart, callbackCatch)(args);
}

const actionResult = (await action(args)) as ActionReturnActionResult;
// Undefined return happens when action redirects/has uncaught error.
const actionResult = (await action(args)) as
| ActionReturnActionResult
| undefined;
if (actionResult === undefined) {
return;
}
const res = actionResultToResult(actionResult) as ActionReturnResult;

setResult(res);
Expand Down

0 comments on commit 407e0aa

Please sign in to comment.