[PR #4669] [MERGED] fix: make sure fetch function doesn't get called repeatedly on onMount #31119

Closed
opened 2026-04-17 22:03:02 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/4669
Author: @frectonz
Created: 9/15/2025
Status: Merged
Merged: 9/15/2025
Merged by: @himself65

Base: canaryHead: fix/fetch-function-called-repeatedly


📝 Commits (2)

  • 26b52fe fix: make sure fetch function doesn't get called repeatedly onMount
  • 1b8dfe3 fix: clear timeout when mount gets destroyed

📊 Changes

1 file changed (+6 additions, -3 deletions)

View changed files

📝 packages/better-auth/src/client/query.ts (+6 -3)

📄 Description

closes #4609

When a NextJS app is run in production mode with 3G throttling the onMount callback gets called multiple times, and that causes the fetch function to be called multiple times. This PR fixes that issue by making sure that the fetch function only gets called on the first onMount callback.

NOTE: The onMount function doesn't get called multiple times during dev mode and if throttling is disabled.


Summary by cubic

Ensure the fetch function runs only once on mount in useAuthQuery by guarding fn() with an isMounted flag. Prevents duplicate requests and potential race conditions (closes #4609).


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/4669 **Author:** [@frectonz](https://github.com/frectonz) **Created:** 9/15/2025 **Status:** ✅ Merged **Merged:** 9/15/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/fetch-function-called-repeatedly` --- ### 📝 Commits (2) - [`26b52fe`](https://github.com/better-auth/better-auth/commit/26b52fea64101b474a485a60a126123f5801fb9e) fix: make sure fetch function doesn't get called repeatedly `onMount` - [`1b8dfe3`](https://github.com/better-auth/better-auth/commit/1b8dfe39d4de26c79c8ba988398f0dbd4e3a04f4) fix: clear timeout when mount gets destroyed ### 📊 Changes **1 file changed** (+6 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/client/query.ts` (+6 -3) </details> ### 📄 Description closes #4609 When a NextJS app is run in production mode with `3G` throttling the `onMount` callback gets called multiple times, and that causes the fetch function to be called multiple times. This PR fixes that issue by making sure that the fetch function only gets called on the first `onMount` callback. NOTE: The `onMount` function doesn't get called multiple times during dev mode and if throttling is disabled. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Ensure the fetch function runs only once on mount in useAuthQuery by guarding fn() with an isMounted flag. Prevents duplicate requests and potential race conditions (closes #4609). <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-17 22:03:02 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#31119