Skip to content

Add flow contracts to apply invalidations on the server - #2

Merged
javaquasar merged 2 commits into
mainfrom
claude/flow-contract
Dec 2, 2026
Merged

javaquasar merged 2 commits into
mainfrom
claude/flow-contract

Conversation

@javaquasar

@javaquasar javaquasar commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Summary

Until now, invalidate and consistency in createFlow were metadata only. They appeared in events and devtools but never touched the Next cache. The real invalidation had to be written a second time inside the Server Action, and the two lists could drift apart. The Next.js example had exactly this drift: the server invalidated /todos/<id> while the client reported /todos/byId/<id>.

This PR adds flow contracts, so the cache effects of a mutation are declared once:

// lib/todo-contract.ts (shared, no directive)
export const createTodoContract = defineFlowContract<CreateTodoInput, Todo>({
  consistency: ({ result }) =>
    consistency.immediate({ tags: [tags.todos.list(), tags.todos.byId(result.id)] }),
});

// app/actions.ts ("use server")
export const createTodoAction = withFlowContract(createTodoContract, insert, { revalidateTag, revalidatePath });

// lib/flows.ts ("use client")
export const createTodoFlow = createFlow({ action: createServerActionAdapter(createTodoAction), ...createTodoContract });

Changes

  • mutaflow: defineFlowContract, resolveFlowContract, and the FlowContract and ResolvedFlowContract types. runFlow now uses the shared resolver.
  • mutaflow/next/server: withFlowContract(contract, action, bindings, { pathType, onApplied }) and createNextServerHelpers(...).withContract(...). The contract is applied only after the action succeeds, and the action receives only input.
  • tags / paths builders are typed recursively. Nested segments used to be unknown, so tags.todos.byId(id) did not typecheck.
  • examples/next-app uses a shared contract. It typechecks and builds again: it used to import updateTag, which Next.js 15 does not export.
  • README Quick Start, concepts guide, example README, and changelog are updated.
  • Includes a changelog correction for Fix runFlow error handling, useFlow lifecycle, and Next helpers #1 (merged before it was pushed): a live Next.js 15.5 check showed that forwarding AbortSignal from the old server action adapter did not throw, so the entry no longer calls it a serialization failure.

Test plan

  • npm test: 44/44 (5 new tests, including one that checks the server and the client report the same invalidations)
  • npm run typecheck
  • examples/next-app: tsc --noEmit and next build on Next.js 15.5
  • Ran the example and created a todo. The server log (onApplied) and the devtools panel show identical invalidations: todos.list, todos.byId.<id>, /todos/<id>

🤖 Generated with Claude Code

Artur Buzov and others added 2 commits December 2, 2026 11:57
A live Next.js 15.5 check showed that forwarding the AbortSignal did
not throw, so describe the change as removing a misleading argument
rather than fixing a serialization failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Declare invalidate/consistency once with defineFlowContract in a module
shared by server and client. withFlowContract (mutaflow/next/server)
applies the contract after the server action succeeds; the client flow
spreads the same contract so events and devtools report exactly what
the server invalidated. runFlow and the server wrapper share
resolveFlowContract, so the lists cannot drift.

- Type tags/paths builders recursively so nested segments are builders.
- Move examples/next-app to a shared contract; drop updateTag, which
  Next.js 15 does not export, so the example typechecks and builds.
- Update READMEs, concepts guide and changelog; add tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@javaquasar
javaquasar changed the base branch from claude/repository-analysis-improvements-7e17f9 to main December 2, 2026 10:59
@javaquasar
javaquasar merged commit c954931 into main Dec 2, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant