Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-deterministic-request-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-fetch": patch
---

Replace `Math.random()` with a monotonic counter in `randomID()`. `Math.random()` is a non-deterministic side effect that can interfere with frameworks that cache or deduplicate `fetch()` calls (e.g. Next.js React Server Components). The counter produces unique IDs within a process while being deterministic across runs.
11 changes: 8 additions & 3 deletions packages/openapi-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ const supportsRequestInitExt = () => {
);
};

/** Counter for deterministic request ID generation */
let _idCounter = 0;

/**
* Returns a cheap, non-cryptographically-secure random ID
* Courtesy of @imranbarbhuiya (https://github.com/imranbarbhuiya)
* Returns a cheap, non-cryptographically-secure unique ID.
* Uses a monotonic counter instead of Math.random() so IDs are
* deterministic within a process, which is compatible with
* frameworks that cache or deduplicate fetch calls (e.g. Next.js).
*/
export function randomID() {
return Math.random().toString(36).slice(2, 11);
return (++_idCounter).toString(36);
}

/**
Expand Down
Loading