NextJS Server Components

Data Fetching: Server Actions and Mutations | Next.js

Do use

  • when you do a simple API call

Do not use

  • When you need HTTP functionality like streaming/websockets

Tips

  • Only return what you must
  • Code that should only ever execute on the server can be marked with: import 'server-only';
  • Server Actions are serialized if they can and then sent to the client

Prefer NEXT Server Components over APIs

Prefer using NEXT Server Components instead of creating an internal API. Caveat: middleware does not handle auth in server actions Use Case:

  • acessing your database
  • make a call to an external service which requires an API token you only have in your backend code.