[PR #2776] [MERGED] fix: SSR handling in useAuthQuery to prevent hydration issues #4481

Closed
opened 2026-03-13 11:48:41 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/2776
Author: @yerzham
Created: 5/24/2025
Status: Merged
Merged: 5/27/2025
Merged by: @Bekacru

Base: mainHead: fix/client-hydration


📝 Commits (2)

  • bf83d69 fix: enhance SSR handling in useAuthQuery to prevent hydration issues
  • 228dd09 simplify the ssr handling

📊 Changes

1 file changed (+18 additions, -11 deletions)

View changed files

📝 packages/better-auth/src/client/query.ts (+18 -11)

📄 Description

Fixes https://github.com/better-auth/better-auth/issues/2462

Fixes the useAuthQuery function in packages/better-auth/src/client/query.ts to be compatible with server-side rendering and prevent hydration issues.

As a result, server always renders with isPending: true and keeps the client initialize with isPending: true as well. Session fetch only happens after nanostores is mounted on client browser. A small delay is added on browser to prevent immediate isPending: false in case session fetch immediately errors and what not.

Changes

  • Skipping fetch on the server
  • Safe initial fetch on client on nanostores mount

🔄 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/2776 **Author:** [@yerzham](https://github.com/yerzham) **Created:** 5/24/2025 **Status:** ✅ Merged **Merged:** 5/27/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `main` ← **Head:** `fix/client-hydration` --- ### 📝 Commits (2) - [`bf83d69`](https://github.com/better-auth/better-auth/commit/bf83d69dc8e14b87699110c18a93fc5130df331e) fix: enhance SSR handling in useAuthQuery to prevent hydration issues - [`228dd09`](https://github.com/better-auth/better-auth/commit/228dd09ed3075a7debe73b89704260d096acc801) simplify the ssr handling ### 📊 Changes **1 file changed** (+18 additions, -11 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/client/query.ts` (+18 -11) </details> ### 📄 Description Fixes https://github.com/better-auth/better-auth/issues/2462 Fixes the `useAuthQuery` function in `packages/better-auth/src/client/query.ts` to be compatible with server-side rendering and prevent hydration issues. As a result, server always renders with `isPending: true` and keeps the client initialize with `isPending: true` as well. Session fetch only happens after nanostores is mounted on client browser. A small delay is added on browser to prevent immediate `isPending: false` in case session fetch immediately errors and what not. ### Changes - Skipping fetch on the server - Safe initial fetch on client on nanostores mount --- <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-03-13 11:48:41 -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#4481