Skip to content

Replace custom server action guards with next-safe-action #11

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

Merged

Conversation

ben-fornefeld
Copy link
Member

@ben-fornefeld ben-fornefeld commented Mar 17, 2025

Replace custom server action guards with next-safe-action

Summary

This PR replaces our custom guard abstraction for server actions with the next-safe-action library, providing improved type safety, error handling, and client-side state management while reducing maintenance burden.

Motivation

Our current approach using custom HOF-based guards worked but required ongoing maintenance and didn't provide optimal client-side state handling. The next-safe-action library offers a more robust, well-maintained alternative with additional features:

  • Chainable middleware pattern for composable action behaviors
  • Built-in error handling and logging
  • Consistent typed responses across all actions
  • Client-side hooks for optimistic updates and loading states

Changes

Server-side

  • Created a base action client with error handling and logging middleware
  • Implemented authActionClient that extends the base client with auth checks
  • Added .metadata() to all actions for improved observability
  • Migrated all server actions from custom guard to next-safe-action pattern

Client-side

  • Replaced startTransition and tanstack query mutation hooks with useAction hook
  • Updated UI components to handle the standardized result schema

Other Issues fixed

  • user & teams fetching now done on the server side
  • network banner layouting

Copy link

linear bot commented Mar 17, 2025

Copy link

vercel bot commented Mar 17, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 17, 2025 2:35pm
web-juliett ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 17, 2025 2:35pm

@ben-fornefeld ben-fornefeld self-assigned this Mar 17, 2025
@ben-fornefeld ben-fornefeld added enhancement New feature or request feature labels Mar 17, 2025
Copy link
Member

@jakubno jakubno left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of PR is misguiding, it isn't only about server action guards, there are also changes unrelated to that. This makes it very hard to review

@ben-fornefeld
Copy link
Member Author

ben-fornefeld commented Mar 17, 2025

The name of PR is misguiding, it isn't only about server action guards, there are also changes unrelated to that. This makes it very hard to review

fair, 90% is about the server action guards. the problem is more that the state management in the component had to be rewritten as soon as the action implementation changed. so it was big from the beginning

@ben-fornefeld ben-fornefeld merged commit 475d4aa into main Mar 17, 2025
4 checks passed
@ben-fornefeld ben-fornefeld deleted the refactor-server-actions-to-next-safe-action-e2b-1810 branch March 17, 2025 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants