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
}
} |
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: