[GH-ISSUE #8910] Replace custom useAuthQuery with nanoquery for built-in revalidateOnFocus, deduplication, caching #19865

Open
opened 2026-04-15 19:13:21 -05:00 by GiteaMirror · 2 comments
Owner

Originally created by @mynk-tmr on GitHub (Apr 2, 2026).
Original GitHub issue: https://github.com/better-auth/better-auth/issues/8910

Originally assigned to: @ping-maxwell on GitHub.

Problem

The current useAuthQuery in client/query.ts is a custom nanostores atom implementation that manually handles fetching, error states, and signal-based refetching. It lacks features that mature query libraries provide:

  • revalidateOnFocus (refetch when window regains focus)
  • revalidateOnReconnect (refetch when network restores)
  • dedupeTime (avoid duplicate requests for the same key)
  • cacheLifetime (stale-while-revalidate)
  • revalidateInterval (polling)

Because of this, consumers who need these features (especially refetchOnWindowFocus) end up wrapping better-auth calls in React Query / TanStack Query, duplicating the query layer. See also #4979.

Proposal

Replace the custom useAuthQuery with nanoquery (@nanostores/query), which is the official query layer for nanostores. It supports all the options above and is a natural fit since better-auth already depends on nanostores.

The createFetcherStore from nanoquery accepts these options:

{
  revalidateOnFocus: true,
  revalidateOnReconnect: true,
  dedupeTime: 4000,
  cacheLifetime: Infinity,
}

This would make the built-in atoms (useActiveOrganization, useSession, listOrganizations, etc.) production-ready without requiring consumers to add a second query layer.

Context

We maintain a cross-platform monorepo (React web + React Native) using better-auth's organization plugin. We currently wrap all better-auth calls in React Query specifically because the built-in atoms lack refetchOnWindowFocus. Adopting nanoquery upstream would let us use the built-in atoms directly.

Originally created by @mynk-tmr on GitHub (Apr 2, 2026). Original GitHub issue: https://github.com/better-auth/better-auth/issues/8910 Originally assigned to: @ping-maxwell on GitHub. ## Problem The current `useAuthQuery` in `client/query.ts` is a custom nanostores atom implementation that manually handles fetching, error states, and signal-based refetching. It lacks features that mature query libraries provide: - `revalidateOnFocus` (refetch when window regains focus) - `revalidateOnReconnect` (refetch when network restores) - `dedupeTime` (avoid duplicate requests for the same key) - `cacheLifetime` (stale-while-revalidate) - `revalidateInterval` (polling) Because of this, consumers who need these features (especially `refetchOnWindowFocus`) end up wrapping better-auth calls in React Query / TanStack Query, duplicating the query layer. See also #4979. ## Proposal Replace the custom `useAuthQuery` with [nanoquery](https://github.com/nanostores/query) (`@nanostores/query`), which is the official query layer for nanostores. It supports all the options above and is a natural fit since better-auth already depends on nanostores. The `createFetcherStore` from nanoquery accepts these options: ```ts { revalidateOnFocus: true, revalidateOnReconnect: true, dedupeTime: 4000, cacheLifetime: Infinity, } ``` This would make the built-in atoms (`useActiveOrganization`, `useSession`, `listOrganizations`, etc.) production-ready without requiring consumers to add a second query layer. ## Context We maintain a cross-platform monorepo (React web + React Native) using better-auth's organization plugin. We currently wrap all better-auth calls in React Query specifically because the built-in atoms lack `refetchOnWindowFocus`. Adopting nanoquery upstream would let us use the built-in atoms directly.
GiteaMirror added the enhancement label 2026-04-15 19:13:21 -05:00
Author
Owner

@mynk-tmr commented on GitHub (Apr 2, 2026):

Additional design consideration: per-hook and client-level options

Beyond replacing the internals, it would be valuable to expose nanoquery options to consumers at two levels:

Per-hook options

Plugin authors and consumers should be able to pass query options per hook:

authClient.useActiveOrganisation({
  revalidateOnFocus: true,
  dedupeTime: 10_000,
})

Client-level defaults

Consumers should be able to set defaults at createAuthClient level:

createAuthClient({
  baseURL: "...",
  query: {
    revalidateOnFocus: true,
    revalidateOnReconnect: true,
    dedupeTime: 4000,
  },
})

Per-hook options override client-level defaults. This gives consumers full control without needing to wrap everything in React Query / TanStack Query.

<!-- gh-comment-id:4175869278 --> @mynk-tmr commented on GitHub (Apr 2, 2026): ## Additional design consideration: per-hook and client-level options Beyond replacing the internals, it would be valuable to expose nanoquery options to consumers at two levels: ### Per-hook options Plugin authors and consumers should be able to pass query options per hook: ```ts authClient.useActiveOrganisation({ revalidateOnFocus: true, dedupeTime: 10_000, }) ``` ### Client-level defaults Consumers should be able to set defaults at `createAuthClient` level: ```ts createAuthClient({ baseURL: "...", query: { revalidateOnFocus: true, revalidateOnReconnect: true, dedupeTime: 4000, }, }) ``` Per-hook options override client-level defaults. This gives consumers full control without needing to wrap everything in React Query / TanStack Query.
Author
Owner

@ping-maxwell commented on GitHub (Apr 2, 2026):

Agreed that this should be supported.
Some of the functionality like refetchOnFocus and such are already supported in the session hooks, they should probably be moved to a higher level so that other hooks can leverage this.

<!-- gh-comment-id:4176337895 --> @ping-maxwell commented on GitHub (Apr 2, 2026): Agreed that this should be supported. Some of the functionality like `refetchOnFocus` and such are already supported in the session hooks, they should probably be moved to a higher level so that other hooks can leverage this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#19865