Skip to content

Commit 6a52d8e

Browse files
authored
Merge pull request #1 from baileympearson/do-not-cache-promise
Cache mongo client, not client promise
2 parents cec8faa + c08bc96 commit 6a52d8e

File tree

5 files changed

+13
-20
lines changed

5 files changed

+13
-20
lines changed

lib/mongodb.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,24 @@ if (!process.env.MONGODB_URI) {
77
const uri = process.env.MONGODB_URI;
88
const options = {};
99

10-
let client;
11-
let clientPromise: Promise<MongoClient>;
10+
let client: MongoClient;
1211

1312
if (process.env.NODE_ENV === "development") {
1413
// In development mode, use a global variable so that the value
1514
// is preserved across module reloads caused by HMR (Hot Module Replacement).
1615
let globalWithMongo = global as typeof globalThis & {
17-
_mongoClientPromise?: Promise<MongoClient>;
16+
_mongoClient?: MongoClient;
1817
};
1918

20-
if (!globalWithMongo._mongoClientPromise) {
21-
client = new MongoClient(uri, options);
22-
globalWithMongo._mongoClientPromise = client.connect();
19+
if (!globalWithMongo._mongoClient) {
20+
globalWithMongo._mongoClient = new MongoClient(uri, options);
2321
}
24-
clientPromise = globalWithMongo._mongoClientPromise;
22+
client = globalWithMongo._mongoClient;
2523
} else {
2624
// In production mode, it's best to not use a global variable.
2725
client = new MongoClient(uri, options);
28-
clientPromise = client.connect();
2926
}
3027

31-
// Export a module-scoped MongoClient promise. By doing this in a
28+
// Export a module-scoped MongoClient. By doing this in a
3229
// separate module, the client can be shared across functions.
33-
export default clientPromise;
30+
export default client;

pages/api/movies.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import clientPromise from "../../lib/mongodb";
1+
import client from "../../lib/mongodb";
22
import { NextApiRequest, NextApiResponse } from 'next';
33

44
export default async (req: NextApiRequest, res: NextApiResponse) => {
55
try {
6-
const client = await clientPromise;
76
const db = client.db("sample_mflix");
87
const movies = await db
98
.collection("movies")

pages/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Head from "next/head";
2-
import clientPromise from "../lib/mongodb";
2+
import client from "../lib/mongodb";
33
import type { InferGetServerSidePropsType, GetServerSideProps } from "next";
44

55
type ConnectionStatus = {
@@ -10,8 +10,8 @@ export const getServerSideProps: GetServerSideProps<
1010
ConnectionStatus
1111
> = async () => {
1212
try {
13-
await clientPromise;
14-
// `await clientPromise` will use the default database passed in the MONGODB_URI
13+
await client.connect();
14+
// `await client.connect()` will use the default database passed in the MONGODB_URI
1515
// However you can use another database (e.g. myDatabase) by replacing the `await clientPromise` with the following code:
1616
//
1717
// `const client = await clientPromise`

pages/movies.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import clientPromise from "../lib/mongodb";
1+
import client from "../lib/mongodb";
22
import { GetServerSideProps } from 'next';
33

44
interface Movie {
@@ -36,7 +36,6 @@ export default Movies;
3636

3737
export const getServerSideProps: GetServerSideProps = async () => {
3838
try {
39-
const client = await clientPromise;
4039
const db = client.db("sample_mflix");
4140
const movies = await db
4241
.collection("movies")

pages/top.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ObjectId } from "mongodb";
2-
import clientPromise from "../lib/mongodb";
2+
import client from "../lib/mongodb";
33
import { GetStaticProps } from "next";
44

55
interface Movie {
@@ -35,8 +35,6 @@ export default function Top({ movies }: TopProps) {
3535

3636
export const getStaticProps: GetStaticProps<TopProps> = async () => {
3737
try {
38-
const client = await clientPromise;
39-
4038
const db = client.db("sample_mflix");
4139

4240
const movies = await db

0 commit comments

Comments
 (0)