[PR #5528] [MERGED] fix(session): refresh cache before it expires #6063

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/5528
Author: @himself65
Created: 10/23/2025
Status: Merged
Merged: 10/23/2025
Merged by: @himself65

Base: canaryHead: himself65/2025/10/23/session


📝 Commits (8)

📊 Changes

7 files changed (+357 additions, -30 deletions)

View changed files

📝 packages/better-auth/src/__snapshots__/init.test.ts.snap (+1 -0)
📝 packages/better-auth/src/api/routes/session-api.test.ts (+250 -13)
📝 packages/better-auth/src/api/routes/session.ts (+72 -12)
📝 packages/better-auth/src/cookies/index.ts (+8 -3)
📝 packages/better-auth/src/init.ts (+10 -0)
📝 packages/core/src/types/context.ts (+1 -0)
📝 packages/core/src/types/init-options.ts (+15 -2)

📄 Description

Summary by cubic

Add freshness-based refresh for the cookie session cache to prevent stale data and reduce unnecessary DB calls. Sessions now refresh before they expire and support cookie-only scenarios.

  • New Features
    • Added session.cookieCache.freshCache (false | true | number) with a default of 60s to control when cached data should be refreshed.
    • Stored updatedAt in the cookie cache and surfaced cookieFreshCache in context for internal use.
    • Updated getSession to:
      • Use cookie data if not expired and still fresh.
      • Refresh from DB when stale, and delete the cookie when expired.
      • Reissue the cookie from payload when DB is unavailable (cookie-only support).
    • Added tests for freshness behavior, disabled mode, no-DB path, and JWT strategy.

🔄 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/5528 **Author:** [@himself65](https://github.com/himself65) **Created:** 10/23/2025 **Status:** ✅ Merged **Merged:** 10/23/2025 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `himself65/2025/10/23/session` --- ### 📝 Commits (8) - [`70c2c48`](https://github.com/better-auth/better-auth/commit/70c2c488af92d61c07263a951bf9b854dd6b0168) fix(session): refresh cache before it expires - [`0e581d7`](https://github.com/better-auth/better-auth/commit/0e581d78df30d98a14bef31b7fbdb6d3555af698) fix: type - [`e1bce26`](https://github.com/better-auth/better-auth/commit/e1bce26f3fa5f5328edbdac23a78b191be2a9009) fix: early return - [`b626a9b`](https://github.com/better-auth/better-auth/commit/b626a9b57ddd0948805a1bb9823d21704b7ee1bf) fix: early return - [`2b468f5`](https://github.com/better-auth/better-auth/commit/2b468f5b32d7f1bb8f10542d841fc562becb118f) chore: fix type - [`2c5920d`](https://github.com/better-auth/better-auth/commit/2c5920d75f85b16ed39b6275e8baf9c707a3e6f4) chore: fix type - [`63b7e5e`](https://github.com/better-auth/better-auth/commit/63b7e5eda39a623ef96b2d75e92f33e21e035307) fix: default value - [`4147423`](https://github.com/better-auth/better-auth/commit/41474233f2de65451b97e4c780faad8e28b170a3) chore: update snap ### 📊 Changes **7 files changed** (+357 additions, -30 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/__snapshots__/init.test.ts.snap` (+1 -0) 📝 `packages/better-auth/src/api/routes/session-api.test.ts` (+250 -13) 📝 `packages/better-auth/src/api/routes/session.ts` (+72 -12) 📝 `packages/better-auth/src/cookies/index.ts` (+8 -3) 📝 `packages/better-auth/src/init.ts` (+10 -0) 📝 `packages/core/src/types/context.ts` (+1 -0) 📝 `packages/core/src/types/init-options.ts` (+15 -2) </details> ### 📄 Description <!-- This is an auto-generated description by cubic. --> ## Summary by cubic Add freshness-based refresh for the cookie session cache to prevent stale data and reduce unnecessary DB calls. Sessions now refresh before they expire and support cookie-only scenarios. - **New Features** - Added session.cookieCache.freshCache (false | true | number) with a default of 60s to control when cached data should be refreshed. - Stored updatedAt in the cookie cache and surfaced cookieFreshCache in context for internal use. - Updated getSession to: - Use cookie data if not expired and still fresh. - Refresh from DB when stale, and delete the cookie when expired. - Reissue the cookie from payload when DB is unavailable (cookie-only support). - Added tests for freshness behavior, disabled mode, no-DB path, and JWT strategy. <!-- 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-03-13 12:46:11 -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#6063