next-action-backpressure
v0.1.1
Published
Serialize rapid Next.js Server Action calls with shared or scoped queues.
Downloads
26
Maintainers
Readme
next-action-backpressure
Queue rapid Next.js Server Action calls instead of letting them execute concurrently.
Why
Repeated clicks on a button can trigger multiple overlapping Server Actions. That can lead to duplicate writes, race conditions, and confusing optimistic UI state. This package adds local backpressure so actions run one at a time.
Install
npm install next-action-backpressureFeatures
- Serializes actions with a shared or local queue
- Supports global queues keyed by feature name
- Includes a React hook for client components
- Optional queue strategies for replace or drop behavior
- Ships typed ESM output with no extra runtime dependencies beyond your app's React stack
Example
'use client';
import { useQueuedAction } from 'next-action-backpressure';
export function SaveButton({
saveDraft
}: {
saveDraft: (formData: FormData) => Promise<void>;
}) {
const { run, isRunning, pendingCount } = useQueuedAction(saveDraft, {
queueKey: 'draft-save'
});
return (
<button disabled={isRunning} onClick={() => void run(new FormData())}>
{isRunning ? `Saving (${pendingCount} queued)` : 'Save draft'}
</button>
);
}API
useQueuedAction(action, options)
Returns:
run: queued version of your async actionisRunning: whether a task is currently executingpendingCount: number of queued calls waiting behind the current oneactiveCount: total running + queued callsqueue: underlyingActionQueue
Options:
queueKey: share a queue across componentsqueue: provide a custom queue instancestrategy: one ofenqueue,replace-pending, ordrop-while-busyonError: observe rejected executions
queueAction(action, options)
Wrap a function outside React and return a queued version.
getActionQueue(queueKey)
Get or create a named global queue.
Compatibility
- Node.js
>=18 - Next.js
14,15, and16 - React
18and19
Reliability
- Unit-tested queue behavior for serialized execution, replacement, and busy-drop modes
- CI runs on every push and pull request
- CodeQL and Dependabot configs are included for ongoing maintenance
- Releases are prepared for npm trusted publishing with provenance
Security
Please report security issues through GitHub private vulnerability reporting when enabled, or by following SECURITY.md.
Contributing
See CONTRIBUTING.md for local development and release notes.
License
MIT
