[PR #6361] [MERGED] fix: avoid throwing on client side #32216

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6361
Author: @landoncolburn
Created: 11/27/2025
Status: Merged
Merged: 12/3/2025
Merged by: @Bekacru

Base: canaryHead: fix/async-hooks-client-import


📝 Commits (2)

  • f4cfc96 fix: avoid throwing on client side
  • abe97ae Merge branch 'canary' into fix/async-hooks-client-import

📊 Changes

1 file changed (+31 additions, -22 deletions)

View changed files

📝 packages/core/src/async_hooks/index.ts (+31 -22)

📄 Description

When importing async_hooks on client side, the AsyncLocalStoragePromise throws since AsyncLocalStorage is not available in the browser. This is semi-intended behaviour since AsyncLocalStoragePromise should not be used on the browser, but the throw should take place on usage, not on import (some files may run on both client and server).

I changed the type signature of AsyncLocalStoragePromise to Promise<typeof AsyncLocalStorage | null>, and then throw in the accessor method getAsyncLocalStorage if AsyncLocalStoragePromise is null.

This should resolve https://github.com/better-auth/better-auth/issues/5539


Summary by cubic

Prevent client-side imports of node:async_hooks from throwing by deferring the error to runtime. The error now only occurs when getAsyncLocalStorage is called in the browser. Resolves better-auth/better-auth#5539.

  • Bug Fixes
    • Updated AsyncLocalStoragePromise type to Promise<typeof AsyncLocalStorage | null>.
    • Return null on client (window detected); keep server warnings and throw only when unavailable server-side.
    • Throw a clear error from getAsyncLocalStorage when used in browser; server behavior unchanged.

Written for commit abe97ae310. Summary will update automatically on new commits.


🔄 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/6361 **Author:** [@landoncolburn](https://github.com/landoncolburn) **Created:** 11/27/2025 **Status:** ✅ Merged **Merged:** 12/3/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `canary` ← **Head:** `fix/async-hooks-client-import` --- ### 📝 Commits (2) - [`f4cfc96`](https://github.com/better-auth/better-auth/commit/f4cfc96fc464beb977eceae0d357aea3d1a7b61b) fix: avoid throwing on client side - [`abe97ae`](https://github.com/better-auth/better-auth/commit/abe97ae310af898e515491b37a432f546ceec6a4) Merge branch 'canary' into fix/async-hooks-client-import ### 📊 Changes **1 file changed** (+31 additions, -22 deletions) <details> <summary>View changed files</summary> 📝 `packages/core/src/async_hooks/index.ts` (+31 -22) </details> ### 📄 Description When importing `async_hooks` on client side, the `AsyncLocalStoragePromise` throws since `AsyncLocalStorage` is not available in the browser. This is semi-intended behaviour since `AsyncLocalStoragePromise` should not be used on the browser, but the throw should take place on usage, not on import (some files may run on both client and server). I changed the type signature of `AsyncLocalStoragePromise` to `Promise<typeof AsyncLocalStorage | null>`, and then throw in the accessor method `getAsyncLocalStorage` if `AsyncLocalStoragePromise` is `null`. This should resolve https://github.com/better-auth/better-auth/issues/5539 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Prevent client-side imports of node:async_hooks from throwing by deferring the error to runtime. The error now only occurs when getAsyncLocalStorage is called in the browser. Resolves better-auth/better-auth#5539. - **Bug Fixes** - Updated AsyncLocalStoragePromise type to Promise<typeof AsyncLocalStorage | null>. - Return null on client (window detected); keep server warnings and throw only when unavailable server-side. - Throw a clear error from getAsyncLocalStorage when used in browser; server behavior unchanged. <sup>Written for commit abe97ae310af898e515491b37a432f546ceec6a4. Summary will update automatically on new commits.</sup> <!-- 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 23:03:47 -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#32216