Skip to content

[BUG] next/navigation functions trigger success status and callbacks #325

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

Open
2 tasks done
maltesa opened this issue Feb 19, 2025 · 4 comments · May be fixed by #345
Open
2 tasks done

[BUG] next/navigation functions trigger success status and callbacks #325

maltesa opened this issue Feb 19, 2025 · 4 comments · May be fixed by #345
Labels
bug Something isn't working

Comments

@maltesa
Copy link

maltesa commented Feb 19, 2025

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

The following code logs Success. to the browsers console.

// action.ts
export const userAction  = userActionClient.action(async () => {
  notFound()
})

// Component.tsx
// ...
  const { execute } = useAction(updateAssignmentSettingsAction, {
    onSuccess: () => console.log('Success.'),
    onError: () => console.log('Error'),
  })

Reproduction steps

Call notFound() in a server action.

Expected behavior

I expected the onError callback to fire.

Link to a minimal reproduction of the issue

none

Operating System

macOs

Library version

7.10.4

Next.js version

15.1.5

Node.js version

22

Additional context

No response

@maltesa maltesa added the bug Something isn't working label Feb 19, 2025
@IPJT
Copy link

IPJT commented Feb 20, 2025

Same thing if the device looses network connection. Seems very weird

@TheEdoRan
Copy link
Owner

TheEdoRan commented Mar 3, 2025

This is the correct behavior for all next/navigation functions at the moment, but I can understand that it might be confusing because users expect notFound() and forbidden() to trigger onError() instead. However, I believe redirect() should continue to trigger onSuccess() (as has already been explained in various issues/discussions in the past). What do you think? I could make notFound() and forbidden() trigger onError() in v8, which would be a breaking change.

@maltesa
Copy link
Author

maltesa commented Mar 3, 2025

Yes, right. Didn't think about, that redirect throws too.

What do you think? I could make notFound() and forbidden() trigger onError() in v8, which would be a breaking change.

That sounds sensible to me. I would love to see it in the v8.

Thank you for this great library 👍

TheEdoRan added a commit that referenced this issue Mar 20, 2025
… functions (#336)

This PR introduces a callback in both action and hook contexts, called
`onNavigation`, that is triggered when the user uses a function imported
from `next/navigation` inside the action body.

re #325
@TheEdoRan
Copy link
Owner

Hey! Here's an update on this issue.

v8.0.0-beta.3 has been released, and it introduces a new callback for both actions and hooks, called onNavigation. This callback is triggered by next/navigation functions (redirect(), notFound(), forbidden(), and unauthorized(), as of now).

Additionally, a new hook status, called hasNavigated, has been added. It is set after any next/navigation function is used in the action's server code function.

I'll be updating the beta documentation soon, but feel free to test these changes already if you'd like.

If you do, I'd love to hear your thoughts on the implementation. Personally, I think it's much clearer now, as navigation functions have their own specific callback and status.

@TheEdoRan TheEdoRan linked a pull request Apr 10, 2025 that will close this issue
@TheEdoRan TheEdoRan changed the title [BUG] using notFound() in server action is handled as successful action [BUG] next/navigation functions trigger success status and callbacks Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants