Skip to content

Commit f300c38

Browse files
author
Utkarsh-Web-2023
committed
early 1.4 version released
1 parent c2093a5 commit f300c38

File tree

3 files changed

+51
-59
lines changed

3 files changed

+51
-59
lines changed

app/layout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ export default function RootLayout({
6060
<NextTopLoader
6161
color='#0E78F9'
6262
initialPosition={0.1}
63-
crawlSpeed={100}
63+
crawlSpeed={50}
6464
height={3}
6565
crawl={true}
6666
showSpinner={true}
6767
easing='ease-in-out'
68-
speed={500}
68+
speed={1000}
6969
shadow='0 0 10px #2299DD,0 0 5px #2299DD'
7070
template='<div class="bar" role="bar"><div class="peg"></div></div>
7171
<div class="spinner" role="spinner"><div class="spinner-icon"></div></div>'

lib/db.ts

Lines changed: 44 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,54 @@
1-
let mongoose;
2-
let isConnected = false;
1+
import mongoose, { Mongoose } from "mongoose";
32

4-
if (typeof window === "undefined") {
5-
// Dynamically import mongoose only on the server-side
6-
mongoose = require("mongoose");
3+
const MONGODB_URL = process.env.MONGODB_URL!;
74

8-
const MONGODB_URL = process.env.MONGODB_URL;
9-
10-
interface MongooseConn {
11-
conn: typeof mongoose | null;
12-
promise: Promise<typeof mongoose> | null;
13-
}
14-
15-
let cached: MongooseConn = (global as any).mongoose;
16-
17-
if (!cached) {
18-
cached = (global as any).mongoose = {
19-
conn: null,
20-
promise: null,
21-
};
22-
}
5+
interface MongooseConn {
6+
conn: Mongoose | null;
7+
promise: Promise<Mongoose> | null;
8+
}
239

24-
export const connect = async () => {
25-
if (isConnected) return;
26-
if (cached.conn) {
27-
isConnected = true;
28-
return cached.conn;
29-
}
10+
let cached: MongooseConn = (global as any).mongoose;
3011

31-
if (!MONGODB_URL) {
32-
throw new Error("MONGODB_URL is not defined");
33-
}
12+
if (!cached) {
13+
cached = (global as any).mongoose = {
14+
conn: null,
15+
promise: null,
16+
};
17+
}
3418

35-
try {
36-
cached.promise =
37-
cached.promise ||
38-
mongoose.connect(MONGODB_URL, {
39-
dbName: "nexus",
40-
bufferCommands: false,
41-
connectTimeoutMS: 10000,
42-
});
19+
export let isConnected = false;
4320

44-
cached.conn = await cached.promise;
45-
isConnected = true;
46-
console.log("Database connected successfully");
47-
return cached.conn;
48-
} catch (error) {
49-
isConnected = false;
50-
console.error("Database connection error:", error);
51-
throw error;
52-
}
53-
};
21+
export const connect = async () => {
22+
if (isConnected) return;
23+
if (cached.conn) {
24+
isConnected = true;
25+
return cached.conn;
26+
}
5427

55-
export function checkConnection() {
56-
return isConnected;
28+
if (!MONGODB_URL) {
29+
throw new Error("MONGODB_URL is not defined");
5730
}
58-
} else {
59-
// Provide stubs for browser or environments where mongoose cannot be used
60-
export const connect = async () => {
61-
console.error("Mongoose cannot be used on the client-side.");
62-
};
63-
export function checkConnection() {
64-
return false;
31+
32+
try {
33+
cached.promise =
34+
cached.promise ||
35+
mongoose.connect(MONGODB_URL, {
36+
dbName: "nexus",
37+
bufferCommands: false,
38+
connectTimeoutMS: 10000,
39+
});
40+
41+
cached.conn = await cached.promise;
42+
isConnected = true;
43+
console.log("Database connected successfully");
44+
return cached.conn;
45+
} catch (error) {
46+
isConnected = false;
47+
console.error("Database connection error:", error);
48+
throw error;
6549
}
50+
};
51+
52+
export function checkConnection() {
53+
return isConnected;
6654
}

middleware.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ export const config = {
2121
};
2222

2323
export async function checkDbConnection() {
24-
if (typeof window === "undefined") {
24+
if (
25+
typeof window === "undefined" &&
26+
process.env.NODE_ENV !== "production"
27+
) {
28+
// Dynamically import the DB module only in non-production environments
2529
const { connect, checkConnection } = await import("@/lib/db");
2630
try {
2731
if (!checkConnection()) {

0 commit comments

Comments
 (0)