fix(client-engine-runtime): make NowGenerator lazy to avoid synchronous new Date() calls#28724
Conversation
This commit changes NowGenerator to lazily initialize the date only when generate() is called.
Previously, GeneratorRegistry.snapshot() would create a NowGenerator which synchronously called new Date().
This caused Next.js dynamic usage errors ('used new Date() before accessing either uncached data') even when now() was not used in the query.
Fixes prisma#28588
|
I'm not opposed to this, but I'm not sure it will have any measurable impact to be honest. Is this a theoretical concern or an actual issue you profiled? Also, if you'd like to get this merged, please sign the CLA. |
Thank you for telling me to sign the CLA, This is my first time contributing to a big repository so I didn't knew that. “used new Date() before accessing either uncached data” This happens even when now() is not used in the query. |
This is blocking all the Nextjs builds, for example. #28588 |
|
In our case prevents building the NextJS app. Any page that is making prisma queries is not possible to cache with cacheComponents. And actually those are the pages that we want to use caching. So the measurable impact is big. |
|
Any news follow-up on this? |
|
This is why people move to @drizzle |
The Issue: The GeneratorRegistry.snapshot() method creates a snapshot of all generators, including NowGenerator
The NowGenerator was initializing new Date() in its property declaration, meaning it was called synchronously during every query preparation, even if now() was not used.
The Fix: I modified NowGenerator to be lazy.
The #now field is now initialized as undefined, and new Date() is only called inside the generate()
method if it hasn't been initialized yet.
Fixes #28588