Skip to content
javaquasarPublic

About

The mutation orchestration layer for Next.js Server Actions.

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Mutaflow

CI License: MIT

Mutation orchestration for Next.js Server Actions.

Mutaflow is for the part that starts after an action is callable:

  • optimistic UI
  • rollback and reconcile
  • invalidation and consistency
  • server action orchestration
  • mutation devtools and test helpers

Why Mutaflow

Server Actions are powerful, but production mutation flows still end up spread across optimistic state, invalidation, retries, redirects, error handling, and custom glue code.

Mutaflow gives that lifecycle one place to live.

Visuals

Devtools Overview

Mutaflow Devtools Overview

Devtools Inspector

Mutaflow Devtools Inspector

Packages

Quick Start

Declare the cache effects of a mutation once, in a module shared by the server and the client:

// lib/post-contract.ts (no "use client" / "use server")
import { defineFlowContract } from "mutaflow";
import { consistency, tags, paths } from "mutaflow/next";

export const createPostContract = defineFlowContract<CreatePostInput, Post>({
  consistency: ({ result }) =>
    consistency.immediate({
      tags: [tags.posts.list(), tags.posts.byId(result.id)],
      paths: [paths.posts(result.id)],
    }),
});

The server action applies the contract after it succeeds:

// app/actions.ts
"use server";
import { revalidatePath, revalidateTag, updateTag } from "next/cache";
import { withFlowContract } from "mutaflow/next/server";

export const createPost = withFlowContract(
  createPostContract,
  async (input: CreatePostInput) => db.posts.insert(input),
  { revalidateTag, updateTag, revalidatePath },
);

The client flow uses the same contract, so its events and devtools report exactly what the server invalidated:

// lib/flows.ts
"use client";
import { createFlow, optimistic } from "mutaflow";
import { createServerActionAdapter } from "mutaflow/next";

export const createPostFlow = createFlow({
  action: createServerActionAdapter(createPost),
  ...createPostContract,
  optimistic: optimistic.insert({
    target: "posts:list",
    item: (input) => ({ id: `temp:${input.title}`, title: input.title, pending: true }),
  }),
});

Render it with useOptimisticFlow. The optimistic item is layered on top of the data from your Server Component and replaced by the real post when the revalidated data arrives; there is no client store to hydrate:

"use client";
import { useOptimisticFlow } from "mutaflow/react";

export function Posts({ posts }: { posts: Post[] }) {
  const createPost = useOptimisticFlow(createPostFlow, posts);

  return (
    <>
      <button disabled={createPost.pending} onClick={() => createPost.run({ title: "Hello" })}>
        New post
      </button>
      {createPost.state.map((post) => <PostRow key={post.id} post={post} />)}
    </>
  );
}

What You Get

  • createFlow, runFlow, useFlow, useOptimisticFlow
  • optimistic UI on top of Server Component data through React useOptimistic, with no client store to hydrate
  • flow contracts: cache invalidations declared once and applied by the server action via withFlowContract
  • optimistic resources with rollback and reconcile, typed through defineResource<T>(key)
  • typed forms: useFlowForm on top of useActionState, with Standard Schema parsing and field errors from the client and from next-safe-action
  • a failure protocol for Server Actions: return actionFailure(...) so field errors and messages survive production builds, where Next.js hides thrown error messages
  • lifecycle hooks and middleware
  • a bounded mutation event log with sinks: mutaflow/otel (one span per run) and mutaflow/sentry (breadcrumbs and issues)
  • mutaflow/next, mutaflow/next/server, and mutaflow/next-safe-action
  • @mutaflow/devtools
  • @mutaflow/testkit

How It Works

  • concept guide: docs/CONCEPTS.md
  • includes step-by-step explanations and Mermaid diagrams for flow runtime, optimistic updates, Next cache integration, devtools, and testkit

Comparison

Mutaflow vs next-safe-action

next-safe-action focuses on safe action definition, schemas, and invocation.

Mutaflow focuses on what happens around a mutation after the action exists:

  • optimistic behavior
  • rollback and reconcile
  • invalidation and consistency
  • lifecycle hooks and middleware
  • grouped devtools
  • test assertions for mutation flows

They can work together. Mutaflow is designed to complement next-safe-action, not replace it.

Mutaflow vs Handwritten Server Action Glue

Handwritten glue is flexible, but it usually scatters mutation behavior across:

  • server action bodies
  • optimistic client state
  • retries and cancellations
  • cache invalidation calls
  • logging and error handling

Mutaflow trades some raw freedom for a more inspectable and reusable mutation system.

Examples

Release Readiness

Repository Notes

The repo uses packages/ intentionally so the runtime and ecosystem packages can evolve together.

See also:

About

The mutation orchestration layer for Next.js Server Actions.

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages