Should i use api routes?
Unanswered
Siamese posted this in #help-forum
SiameseOP
So im working on a single vendor ecommerce website, and im using app router so should i just use server actions & server components for CURD operations or should i make CURD for each feature as api route ?
9 Replies
That’s up to you and honestly it will depend on the complexity of the project.
1- I find server actions (mutations) and server components (queries) work well for smaller projects that don’t require scaling, but if you take this path. I recommend you build a Data Layer where you concentrate all the Server Side logic, functions that will perform either the queues or the mutations and simply import these functions into your server components or server actions.
This Data Layer I mention, is useful to later on apply caching in a per function level, or even auth checks.
And if later you need to spin up an API route for any reason, maybe if the data needs to be fetched exclusively from the client, then you can just reuse these utility functions inside the API Route handlers.
Worth adding that, the project is complex enough and needs to scale, and you’re allowed/willing to use a separate backend I’d do that instead of using API routes.
1- I find server actions (mutations) and server components (queries) work well for smaller projects that don’t require scaling, but if you take this path. I recommend you build a Data Layer where you concentrate all the Server Side logic, functions that will perform either the queues or the mutations and simply import these functions into your server components or server actions.
This Data Layer I mention, is useful to later on apply caching in a per function level, or even auth checks.
And if later you need to spin up an API route for any reason, maybe if the data needs to be fetched exclusively from the client, then you can just reuse these utility functions inside the API Route handlers.
Worth adding that, the project is complex enough and needs to scale, and you’re allowed/willing to use a separate backend I’d do that instead of using API routes.
@Siamese so if i use a golang backend with nextjs should i go with app router or pages router?
App router, every time (imo)
@LuisLl App router, every time (imo)
SiameseOP
then if i just use a go backend then how app/ will make sense??
😥 im kinda confused
Using the app router does not directly mean using Server Actions and API routes.
The app router offers a cleaner file based router, nested layouts, route groups, etc.
App router also brings server components, and since these are the default, you can still leverage them. It does not matter if you’re fetching your Go endpoints inside of them.
The app router offers a cleaner file based router, nested layouts, route groups, etc.
App router also brings server components, and since these are the default, you can still leverage them. It does not matter if you’re fetching your Go endpoints inside of them.
Next.js Server Components != Full stack. You can still use Next.js as the frontend with all these built in optimizations, while having a separate backend.
@LuisLl Using the app router does not directly mean using Server Actions and API routes.
The app router offers a cleaner file based router, nested layouts, route groups, etc.
App router also brings server components, and since these are the default, you can still leverage them. It does not matter if you’re fetching your Go endpoints inside of them.
SiameseOP
thanks for clearing my doubts
Hopefully you can take a decision based on your requirements and preferences.