generated from nimblehq/git-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
olivierobert
merged 10 commits into
develop
from
chores/nextjs-v13-refactor-apis-into-server-actions
Jun 26, 2023
Merged
Refactor API endpoints into Server Actions #129
olivierobert
merged 10 commits into
develop
from
chores/nextjs-v13-refactor-apis-into-server-actions
Jun 26, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tednguyendev
changed the title
Chores/nextjs v13 refactor apis into server actions
Refactor apis into server actions
Jun 9, 2023
tednguyendev
force-pushed
the
feature/53-nextjs-v13-frontend-view-newsletter
branch
2 times, most recently
from
June 13, 2023 07:34
e41cfdd
to
fe0f1f7
Compare
tednguyendev
force-pushed
the
feature/53-nextjs-v13-frontend-view-newsletter
branch
5 times, most recently
from
June 22, 2023 09:57
d49b178
to
408e05b
Compare
Base automatically changed from
feature/53-nextjs-v13-frontend-view-newsletter
to
develop
June 23, 2023 06:19
tednguyendev
force-pushed
the
chores/nextjs-v13-refactor-apis-into-server-actions
branch
from
June 23, 2023 09:48
ea66aa7
to
5923673
Compare
tednguyendev
force-pushed
the
chores/nextjs-v13-refactor-apis-into-server-actions
branch
2 times, most recently
from
June 23, 2023 10:17
9998d06
to
cbd94d8
Compare
tednguyendev
force-pushed
the
chores/nextjs-v13-refactor-apis-into-server-actions
branch
from
June 23, 2023 10:31
cbd94d8
to
491d63a
Compare
olivierobert
changed the title
Refactor apis into server actions
Replace API endpoints by Server Actions
Jun 26, 2023
olivierobert
changed the title
Replace API endpoints by Server Actions
Refactor API endpoints into Server Actions
Jun 26, 2023
olivierobert
approved these changes
Jun 26, 2023
There was a problem hiding this 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.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#106 (comment)
What happened 👀
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:
axios
call without our server action(the functionsendNewsletter
in this example)startTransition
(startTransition
is 1 of the 3 ways to use Server Action in Nextjs, along withaction
andformAction
)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:
NextAuth
(when try to get user session on server side, NextAuth raise errors), but luckily, there is this work aroundactions/newsletter.tsx
, I have to create a newactions/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 thisactions/newsletter.tsx
(which only to import actions fromactions.tsx
AND ADD the "use server").Refs
Proof Of Work 📹
N/A