-
Notifications
You must be signed in to change notification settings - Fork 514
fix(auth): replace Math.random with crypto based UUID to support N… #1839
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ext.js 16 SSR Next.js 16 App Router requires deterministic rendering for Server Components. The previous implementation of uuid() in @supabase/auth-js used Math.random(), which caused prerender errors even when no queries were executed. This update replaces Math.random() with crypto.getRandomValues in browsers and crypto.randomFillSync in Node, providing SSR-safe, fully synchronous UUID v4 generation. ### Changes - Refactored uuid() in packages/auth-js/src/lib/helpers.ts - Fully synchronous and UUID v4 compliant - Compatible with Node 16+ and modern browsers - Resolves SSR/deterministic rendering errors caused by Math.random() Closes #40273
|
@7ttp thank you SO much for your contribution! Can you make sure that |
|
I originally tried using crypto.getRandomValues() and the same build fail as Math.random(). Using performance.now() works for Next@16 cacheComponent testing. |
mandarini
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
Thanks for this PR! This is the cryptographically correct approach for UUID generation. However, we've received reports from @BOXNYC (see #1844) that > "Route "/" used [non-deterministic operation] before accessing either uncached data."This suggests the issue isn't just Root CauseThe real problem is that
I need to discuss with the team what the best approach to move forward is. This PR has the right crypto implementation, but we need architectural changes to make it work with Next.js 16. Until we fix this properly, users can defer client initialization to avoid pre-rendering, or disable SSR for the provider component. |
|
Glad to see progress on #1847 |
|
Closing this in favor of #1847 |
Next.js 16 App Router enforces deterministic rendering for Server Components.
The previous uuid() implementation in @supabase/auth-js relied on Math.random(),
which caused prerender errors even when no queries were executed.
This update replaces Math.random() with crypto.getRandomValues in browsers
and crypto.randomFillSync in Node, providing SSR-safe, fully synchronous UUID v4 generation.
Changes
Closes #40273
🔍 Description
Fixes SSR prerender errors in Next.js 16 caused by Math.random() in uuid().
What changed?
Replaced Math.random() with crypto.getRandomValues in browsers and crypto.randomFillSync in Node. Updated uuid() to remain fully synchronous and UUID v4 compliant.
Why was this change needed?
To ensure Server Components render deterministically and prevent Next.js 16 prerender errors.
📸 Screenshots/Examples
N/A — code change only.
🔄 Breaking changes
📋 Checklist
npx nx formatto ensure consistent code formatting📝 Additional notes
This is a low-level crypto fix; no functional changes to existing APIs.