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

Refactor API endpoints into Server Actions #129

Merged

Conversation

tednguyendev
Copy link
Contributor

@tednguyendev tednguyendev commented Jun 9, 2023

#106 (comment)

What happened 👀

  • Add the new actions.tsx file to contains all actions for 4 Server Actions(Create, Update, Delete, Send)
  • Modify client code to use Server Actions instead of calling APIs
  • Add and modify tests
  • Remove all the API codes and tests of 4 actions

Insight 📝

Purpose of PR

As discussed here, this is the PR to refactor all the API code that can be replaced by Next.js Server Actions.

API to Server Actions

It's surprising for me to see that it's very easy to refactor a flow that's using API to using Server Action in Next.js v13.

For example, for this sending newsletter feature, to use Server Action, based on the refactored code here, we just need to:

  • Replace the axios call without our server action(the function sendNewsletter in this example)
  • Wrap the whole calling server action thing in startTransition(startTransition is 1 of the 3 ways to use Server Action in Nextjs, along with action and formAction)
  • Everything else stays the same

Cons

It is easy to use this new Server Action feature, but since this is still a new feature, there are some difficulties I faced while trying it, such as:

  • Not very compatible with NextAuth(when try to get user session on server side, NextAuth raise errors), but luckily, there is this work around
  • When we run tests, it raises some import errors, resulting in having to do a lot of import mock like this to make it works.
  • It also raises errors when running tests if we import server actions from a file that has "use server" to test files. So instead of only having 2 file, actions/newsletter.tsx, I have to create a new actions/actions.tsx file that contains the server actions but not the "use server", and when I need to use the server actions on client components, I use it through this actions/newsletter.tsx(which only to import actions from actions.tsx AND ADD the "use server").

Refs

Proof Of Work 📹

N/A

@tednguyendev tednguyendev changed the title Chores/nextjs v13 refactor apis into server actions Refactor apis into server actions Jun 9, 2023
@tednguyendev tednguyendev self-assigned this Jun 9, 2023
@tednguyendev tednguyendev added this to the 0.4.0 milestone Jun 9, 2023
@tednguyendev tednguyendev force-pushed the feature/53-nextjs-v13-frontend-view-newsletter branch 2 times, most recently from e41cfdd to fe0f1f7 Compare June 13, 2023 07:34
@tednguyendev tednguyendev modified the milestones: 0.4.0, 0.5.0 Jun 16, 2023
@tednguyendev tednguyendev force-pushed the feature/53-nextjs-v13-frontend-view-newsletter branch 5 times, most recently from d49b178 to 408e05b Compare June 22, 2023 09:57
Base automatically changed from feature/53-nextjs-v13-frontend-view-newsletter to develop June 23, 2023 06:19
@tednguyendev tednguyendev force-pushed the chores/nextjs-v13-refactor-apis-into-server-actions branch from ea66aa7 to 5923673 Compare June 23, 2023 09:48
@tednguyendev tednguyendev force-pushed the chores/nextjs-v13-refactor-apis-into-server-actions branch 2 times, most recently from 9998d06 to cbd94d8 Compare June 23, 2023 10:17
@tednguyendev tednguyendev marked this pull request as ready for review June 23, 2023 10:29
@tednguyendev tednguyendev force-pushed the chores/nextjs-v13-refactor-apis-into-server-actions branch from cbd94d8 to 491d63a Compare June 23, 2023 10:31
@olivierobert olivierobert changed the title Refactor apis into server actions Replace API endpoints by Server Actions Jun 26, 2023
@olivierobert olivierobert changed the title Replace API endpoints by Server Actions Refactor API endpoints into Server Actions Jun 26, 2023
Copy link
Contributor

@olivierobert olivierobert left a comment

Choose a reason for hiding this comment

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

Just one minor comment but this looks good.

I am unsure about grouping all actions into the same file (actions.tsx) if the application was larger but the decoupling into a separate directory is a great idea. It looks similar to Redux.

nextjs-13/src/lib/request/getServerSession.ts Show resolved Hide resolved
@olivierobert olivierobert merged commit 388d524 into develop Jun 26, 2023
1 check passed
@olivierobert olivierobert deleted the chores/nextjs-v13-refactor-apis-into-server-actions branch June 26, 2023 03:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants