You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
How are you deploying your application? (if relevant)
next dev
Describe the Bug
When getStaticPaths and getStaticProps await clientPromise in development, a new client connection is established each time the page loads. This causes delays when loading pages and creates unnecessary open connections.
Expected Behavior
The same database connection is reused for subsequent requests.
To Reproduce
Create the file pages/[slug].tsx with the following code:
Create a MongoDB database named myPosts, with a collection called posts. Each post should have a (unique) slug field, a title, and a content field.
Add a console.log to line 17 of lib/mongodb.ts (to make the bug more clear)
import{MongoClient}from'mongodb'if(!process.env.MONGODB_URI){thrownewError('Invalid/Missing environment variable: "MONGODB_URI"')}consturi=process.env.MONGODB_URIconstoptions={}letclientletclientPromise: Promise<MongoClient>if(process.env.NODE_ENV==='development'){// In development mode, use a global variable so that the value// is preserved across module reloads caused by HMR (Hot Module Replacement).if(!global._mongoClientPromise){console.log('Creating new client')// <-- Add this lineclient=newMongoClient(uri,options)global._mongoClientPromise=client.connect()}clientPromise=global._mongoClientPromise}else{// In production mode, it's best to not use a global variable.client=newMongoClient(uri,options)clientPromise=client.connect()}// Export a module-scoped MongoClient promise. By doing this in a// separate module, the client can be shared across functions.exportdefaultclientPromise
In .env.local, add MONGODB_URI and set NODE_ENV=development.
Run yarn dev, and navigate to various post pages. You should see logs like this:
There will also be a noticeable delay before each page loads, if your database is remote.
The text was updated successfully, but these errors were encountered:
This issue has been automatically marked as stale due to two years of inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you.
Uh oh!
There was an error while loading. Please reload this page.
Verify canary release
Provide environment information
Which example does this report relate to?
with-mongodb
What browser are you using? (if relevant)
Firefox 108.0
How are you deploying your application? (if relevant)
next dev
Describe the Bug
When
getStaticPaths
andgetStaticProps
await clientPromise in development, a new client connection is established each time the page loads. This causes delays when loading pages and creates unnecessary open connections.Expected Behavior
The same database connection is reused for subsequent requests.
To Reproduce
pages/[slug].tsx
with the following code:myPosts
, with a collection calledposts
. Each post should have a (unique)slug
field, atitle
, and acontent
field.console.log
to line 17 oflib/mongodb.ts
(to make the bug more clear).env.local
, addMONGODB_URI
and setNODE_ENV=development
.yarn dev
, and navigate to various post pages. You should see logs like this:There will also be a noticeable delay before each page loads, if your database is remote.
The text was updated successfully, but these errors were encountered: