fix(auth): use Symbols for callback IDs to resolve Next.js 16 compatibility #1847
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Next.js 16 enforces deterministic rendering for Server Components and blocks all non-deterministic operations during pre-rendering, including:
Math.random()crypto.randomUUID()crypto.getRandomValues()This causes build errors when instantiating
createBrowserClient():Root cause: The
uuid()function usesMath.random()to generate unique IDs for auth state change callback subscriptions. WhenGoTrueClientinitializes, it callsonAuthStateChange()which generates these IDs during SSR, triggering Next.js 16's deterministic rendering check.Solution
Replaced
uuid()withgenerateCallbackId()that uses JavaScript Symbols instead of random number generation.Why Symbols?
Symbols are a good solution for internal callback identifiers:
When we call
supabase.auth.onAuthStateChange(callback), the library needs to track the callback among potentially many others. The id is an internal key used in a Map to identify which callback to invoke or remove. So no need for cryptographically secure id's.Changes
Renamed function:
uuid()→generateCallbackId()Implementation: Return
Symbol('auth-callback')instead of uuid stringType updates:
Subscription.idchanged fromstringtostring | symbolDocumentation: Clarified that
idis an internal identifier not meant for application useAcknowledgements
Thanks to @7ttp and @BOXNYC for raising the issue and suggesting solutions!