Skip to content
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

[BUG] onError called on redirects if it was called previously #303

Closed
2 tasks done
berekuk opened this issue Dec 1, 2024 · 4 comments
Closed
2 tasks done

[BUG] onError called on redirects if it was called previously #303

berekuk opened this issue Dec 1, 2024 · 4 comments
Labels
bug Something isn't working released

Comments

@berekuk
Copy link

berekuk commented Dec 1, 2024

Are you using the latest version of this library?

  • I verified that the issue exists in the latest next-safe-action release

Is there an existing issue for this?

  • I have searched the existing issues and found nothing that matches

Describe the bug

When the action redirects, onError callback from useAction parameter will be called again if it was called previously.

This can cause incorrect repeat notifications of stale errors.

I have a form component that does something like this:

  const { executeAsync, status } = useAction(myAction, {
    onError: ({ error }) => {
      toast(error);
    },
  });

  // this is react-hook-form, but shouln't matter
  const onSubmit = form.handleSubmit(executeAsync);

  ...

And an action that can fail based on the form data. When the action succeeds, it calls redirect(...).

The problem is: if the action is called once and resulted in an error, and then it's called again with the correct data, then on the second call onError will be called again, and there'll be an extra confusing toast for that older error.

I tried to trace this issue through the codebase, and in my understanding it's caused by this code:

safeActionFn(input as S extends Schema ? InferIn<S> : undefined)
.then((res) => setResult(res ?? {}))
.catch((e) => {
throw e;
})

When catch((e) => ... happens because of NEXT_REDIRECT, the result is not reset, so it stays in "hasErrored" state, and then the effect in useActionCallbacks() fires once again with that stale errored result (for some reason; I'm not sure which effect dep causes the callbacks effect to fire).

Reproduction steps

  1. Start the https://github.com/berekuk/next-safe-action-redirect-error
  2. Press "fail".
  3. Check console; there will be the first "ERROR" in logs.
  4. Press "redirect"; see how there's a second "ERROR" being logged on redirect.

Expected behavior

On the second button click, onError shouldn't be called.

Link to a minimal reproduction of the issue

https://github.com/berekuk/next-safe-action-redirect-error

Operating System

macOS

Library version

7.9.9

Next.js version

15.0.3

Node.js version

v20.15.1

Additional context

No response

@berekuk berekuk added the bug Something isn't working label Dec 1, 2024
@IDJGILL
Copy link

IDJGILL commented Jan 21, 2025

I confirm this issue exist.

@hTaw99
Copy link

hTaw99 commented Jan 24, 2025

Exactly same case and i face this issue

Copy link

github-actions bot commented Feb 2, 2025

🎉 This issue has been resolved in version 7.10.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@TheEdoRan
Copy link
Owner

TheEdoRan commented Feb 2, 2025

Hi and sorry for the delay in fixing this issue. The problem was caused by the fact that the result was not properly reset when the action entered the catch block. In other words, when an internal framework error occurred, the result was not cleared. Now, by resetting the result to {} before re-throwing the error, the onSuccess callback function is correctly executed with data set to undefined, just like when a redirect happens immediately. So it should be fixed in v7.10.3, please let me know if that's the case, and thank you for your patience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
None yet
Development

No branches or pull requests

4 participants