fix: defer MongoClient init to request time to fix build crash#100
Open
Harshit-Mishra2212 wants to merge 1 commit into
Open
fix: defer MongoClient init to request time to fix build crash#100Harshit-Mishra2212 wants to merge 1 commit into
Harshit-Mishra2212 wants to merge 1 commit into
Conversation
|
@Harshit-Mishra2212 is attempting to deploy a commit to the SEETA's projects Team on Vercel. A member of the Team first needs to authorize it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📌 Description
new MongoClient(uri)was called at module level incohort/route.tswith a non-null assertion onprocess.env.MONGO_URI. Duringnext build, Next.js evaluates route modules without runtime environment variables, soMONGO_URIis undefined at build time andMongoClientthrows immediately crashing the entire build:TypeError: Cannot read properties of undefined (reading 'startsWith')
at new MongoClient (mongodb/lib/mongo_client.js:52:61)
at app/api/cohort/route.js
Build error occurred: Failed to collect page data for /api/cohort
Reproducible on any clean clone without .env.local - exactly the environment CI pipelines and Vercel production builds run in. The ! non-null assertion only silences TypeScript, it does nothing at runtime.
Additionally, client.connect() was called on every GET request with no connection reuse, opening a new MongoDB connection per request and never closing it exhausting the Atlas connection pool under real load.
Not related to any specific issue but directly affects deployment reliability.
✅ Changes
Fix
Moved MongoClient creation into a getClient() helper that runs only at request time - build no longer crashes when MONGO_URI is absent
Added explicit MONGO_URI presence check that returns a meaningful error message instead of crashing with a TypeError
Added module-level cachedClient so the connected client is reused across requests - standard recommended pattern for MongoDB in Next.js serverless deployments
🔍 Additional Notes
No breaking changes. The GET handler behaviour is identical only the initialisation timing and connection reuse strategy changed. Existing callers of /api/cohort are unaffected.
Checklist