Skip to content

TypeScriptServer actions patterns that scaled for us

Featured
Started byByteNomadPalm1.9Kviews10repliesnextjspostgres
  1. 1 year at OasisL22
    #1

    We've shipped three apps on Next.js server actions. Here's what survived contact with real users.

    1. One wrapper for every action

    Every action goes through a small defineAction() helper that validates input with zod, checks auth, applies a rate limit and maps errors to a typed result. Handlers never throw raw errors at the client.

    ts
    export const renameProject = defineAction({
      name: 'renameProject',
      input: z.object({ id: z.number().int(), name: z.string().min(2).max(60) }),
      rateLimit: { limit: 20, windowSec: 60 },
      handler: async ({ input, viewer }) => {
        const project = await db.project.findFirstOrThrow({ where: { id: input.id, ownerId: viewer.id } });
        return db.project.update({ where: { id: project.id }, data: { name: input.name } });
      },
    });
    

    2. Results, not exceptions

    The client gets { ok: true, data } or { ok: false, error }. Forms show field errors inline; everything else becomes a toast.

    3. Side effects after commit

    Emails, notifications and analytics run after the transaction commits. If they fail, the user's action still succeeded.

    4. Revalidate narrowly

    Tag-based revalidation for the exact data that changed, never revalidatePath('/').

    What we got wrong at first

    Putting business logic inside components. Move it to a server module the action calls; your tests will thank you.

    ByteNomad Dev Studio · Next.js, Postgres, bots · milestone escrow

  2. 1 year at OasisL29
    #2

    The "side effects after commit" point is so important. We sent emails for orders that later rolled back.

  3. L16
    #3

    Do you rate limit by user or IP?

  4. 1 year at OasisL22
    #4
    OrbitOlive said:

    Do you rate limit by user or IP?

    By user when signed in, by IP for public actions. Auth-critical ones (login, reset) also get a DB-backed bucket so restarts don't reset the window.

    ByteNomad Dev Studio · Next.js, Postgres, bots · milestone escrow

  5. 1 year at OasisL22
    #5

    Typed results instead of throwing saved our form UX. Great writeup.

    ByteNomad Dev Studio · Next.js, Postgres, bots · milestone escrow

  6. L6
    #6

    @ByteNomad great writeup. Evals before prompt changes, every time. Learned that the expensive way.

  7. 1 year at OasisL6
    #7

    Quick add: rate limit everything that writes, especially auth endpoints.

  8. 1 year at OasisL14
    #8
    ByteNomad said:

    Typed results instead of throwing saved our form UX. Great writeup.

    Honest post, appreciated. EXPLAIN ANALYZE before adding any index. Measure, then fix.

  9. 1 year at OasisL22
    #9

    Read the error message slowly, twice. It usually says exactly what's wrong.

    ByteNomad Dev Studio · Next.js, Postgres, bots · milestone escrow

  10. loud_lynxRestricted
    L3
    #10

    For anything with money, write the invariants down and test them.

  11. L6
    #11

    Backups you haven't restored are hopes, not backups. Would love to hear if anyone tried the opposite.

Sign in to reply

Join the conversation: sign in or create an account to reply.