-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Next App Router (14.2.3) - Inconsistent Singleton #65350
Comments
Are you sure it's not becaues of the Webpack Build Worker? This can split your build over a few different workers. This means that webpack is run X times over your app and therefore you may see X amount of logs called. You can opt out of it: https://nextjs.org/docs/messages/webpack-build-worker-opt-out#webpack-build-worker-opt-out although you will most likely see a degrade in build times. |
I thought this could be it (as I was already thinking it's running separate builds in parallel), but it doesn't seem to change anything. When I add the following it still runs the initialization multiple times (1 time for every ~20 pages): const nextConfig = {
experimental: {
webpackBuildWorker: false
}
} |
I have the same problem. I tried to use globalThis in dev mode as Prisma's suggestion, but it does not work. |
Same here, doing exactly like this file: https://github.com/vercel/next.js/blob/canary/examples/with-mongodb/lib/mongodb.ts but it's recreated multiple times (twice at the start of the application, 1 other time when calling inside a server action from client, third party component). This happens only on production build |
Same problem. I have the same observations using both a singleton and globalThis. Singleton
globalThis
ServerI created a TRPC procedure to expose the value and increment it on each request.
FrontendAdd button to trigger query on click. Display ObservationThe value is correctly incremented
however The logs for the ConclusionThis is very confusing, because according to the logs, it's broken. I'm guessing that even though singletons and using Is there an explanation for this scenario, or is it a black box? |
I have been researching this at some length, coming at this from a slightly different angle but I think the core issue is the same. The reason you see your initialization logic occurring more than once is due to Next internally using two different module systems, CJS for the server context, and webpack for the browser context. You can see in the Next server there are actually a number of different mechanisms for importing modules. What was useful in my case to understand this was using
And here is what the RSC imports look like:
This is a problem for a lot of other libs which rely on singletons, or otherwise need to use the state of objects which are loaded into the RCS context. In my case this completely breaks prometheus metrics which are used in your component code. But this seems like a pretty fundamentally hard problem to overcome, I'm not sure how you'd avoid re-importing something that needs to exist in multiple module formats. |
@MichaelSitter thanks for digging into that. I've also notice the different module importing issue causing singletons not to work, now I know why. Is this intended or a bug/bad design? Fwiw, using |
Also hitting this, I have a socket with a single connection, but it seems a client component that calls a server action and a server component that calls a server action create different bundles in development, thus globalThis is not shared? |
Hi everyone!— I have discussed this with the First, during build, by default we use multiple workers. These are distinct child processes so unique copies of node that each load the module system and build some subset of pages. So for each worker spawned during the build you will see a copy of singleton initialization. We don't optimize for singletons because it would force us to use only 1 cpu core (JS is single-threaded) when for many machines more are available and thus builds can be faster if we parallelize. Second, cjs vs webpack module loading seems like a bug/misconfiguration. In the first trace the module is being loaded by node which means it was at some point treated as an external. The second trace shows the same module being loaded by webpack so it wasn't treated as an external. This might be because the module is being used in RSC and client and we intentionally fork these modules so you can have one implementation for RSC and one for the client. In the future we may actually render the RSC part in it's own process which again would force there to be two different module instances. The pattern of using a singleton during build or render to have a global view of the state of the server is just something that we are currently not optimizing for. It is maybe possible to mark something as "must be external" which means that it would again be a single instance across RSC and client (on the server). Let me know if that clarifies! |
Thanks for the info @samcx . The first makes sense and is expected, but the 2nd can be confusing and cause unintended bugs. Please let us know when the "must be external" flag is implemented. |
Thank you for bringing this up with the team, @samcx! I've faced this issue in plugin development where I need one instance of the TypeScript compiler and Shiki syntax highlighter which can be very expensive to run. For now, I've opted to have people run a separate process that I can control better to avoid this since it can cause OOM issues, but breaks down the DX. Since this problem seems very similar to the instrumentation hook, it would be great to expose that in the next.js configuration rather than a file so plugin authors could make use of this as well. Something like the following in next config would be great if possible: export default {
instrumentation: ['init-singleton-here.ts']
} |
Link to the code that reproduces this issue
https://github.com/vaneenige/next-app-router-singleton
To Reproduce
npm run build
Create random
multiple timesCurrent vs. Expected behavior
Current behavior:
Expected behavior:
In the reproduction repository you can find a few different test cases:
After quite a bit of research I found multiple sources claiming that this pattern does work with the
pages
directory, but also multiple sources that can't get it to work with theapp
directory.globalThis
method inpages
: Global variables #15054 (comment)app
: Caching in Dev Mode #48481 (comment)globalThis
solution to prevent too many connections: https://www.prisma.io/docs/orm/more/help-and-troubleshooting/help-articles/nextjs-prisma-client-dev-practicesHaving a proper singleton could benefit a lot of use cases such as database connections or separate worker scripts.
I've seen the issue being discussed on Reddit and GitHub with regards to MongoDB (or other persistent API connections) too.
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:34 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T8103 Available memory (MB): 16384 Available CPU cores: 8 Binaries: Node: 18.17.1 npm: 9.6.7 Yarn: N/A pnpm: N/A Relevant Packages: next: 14.2.3 // Latest available version is detected (14.2.3). eslint-config-next: 14.2.3 react: 18.3.1 react-dom: 18.3.1 typescript: 5.4.5 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Module Resolution
Which stage(s) are affected? (Select all that apply)
next build (local)
Additional context
No response
The text was updated successfully, but these errors were encountered: