1

Design Pattern Question Rest APIs vs Server Actions (Next.js App Router)

Profile picture
Mid-Level Software Engineer at Taro Community12 days ago

Background

I have started developing an internal task management system based on server actions. The problem is that server actions cannot run in parallel. At some screens using Parallel routes, I am calling more than one server action, and since they run sequentially, it’s hurting performance.

Question

What would be the best course of action here, I found a library that can be used to run server action in parallel While I was reading somewhere that one should not use server actions for GET requests and only for mutations, based on this I am thinking about going to ‘api’ based routes for GET requests and keep the post requests as server actions. Need advice from seniors here who have worked in NextJs before.

Also having a codebase following two kinds of approaches server action and API routes seems weird to me because as per my limited knowledge plus what Alex taught in his Code Quality Course, one should follow one pattern in a codebase so that it stays consistent. Would appreciate the help or any kind of advice here.

22
2

Discussion

(2 comments)
  • 0
    Profile picture
    Eng @ Taro
    10 days ago

    Just to preface, we build the Taro webapp using Next.js, but we don't use server actions. Not that it was intentional, it just ended up working out that way. It was experimental with the Next.js version that we were using. I haven't personally used server actions before, so it might be useful to get some input from someone else that has used them before, too.

    What would be the best course of action here, I found a library that can be used to run server action in parallel While I was reading somewhere that one should not use server actions for GET requests and only for mutations

    Once again, I've only read about server actions. It sounds like they should only be used for mutating data. I am really hesitant about using libraries in a way that they weren't meant to be used.

    If I was building a new webapp, I would probably still use a REST API (at least for an enterprise app) because it's tried and true:

    • There's a whole standard for how to design REST endpoint urls.
    • Most logging, monitoring, and observability services are built for REST APIs.
    • Having a separate backend API enforces the concept of having separation of concerns.
    • It's easier to open up your API to other consumers in the future (mobile app or third party developers)

    one should follow one pattern in a codebase so that it stays consistent. Would appreciate the help or any kind of advice here.

    You can try to use the library to allow data fetching to keep everything consistent but just know that there are tradeoffs. It just seems very "code-smelly" to be using a hack to get server actions to work in a way that it wasn't meant to work. You might be able to get it to work now, but it's always hard to tell whether this will cascade into more hacks in the future. And, it makes it harder to migrate off of in the future.

    I think if your app is going to remain lightweight for the future, you can go with the server action path. At least you'll know if you go down this path, it will be easier to migrate later if you need to. But, if you're expecting to build a platform for many clients in the future, it might be worth it to go the REST API route now.

    • 0
      Profile picture
      Mid-Level Software Engineer [OP]
      Taro Community
      10 days ago

      Yes actually this app is for our internal management purposes plus for clients who will be working with us ( kind of a Management System ) and since I am early in the process ( making an MVP ) I want to make sure it's scalable in the future.