Created web APP in next js page is loading very slow at first time

1.1k views Asked by At

I am currently working on upgrading my server and database to the next 13 versions. When I start the server for the first time, I'm experiencing a significantly slow response time. The initial response time is around 9-10 seconds. However, when I send a request to the same route again, the second response time is under 1 second.

I suspect that the initial delay might be due to setting up the database connection for the first time. But I'm concerned about this affecting all routes. Since I need to establish a connection for each route, does this mean that the first response time will be slow for every route?

Below, I have provided the code for one of the routes. Could there be a solution to this issue?

   import { ConnectDB } from "../../../config/dbconnection";
const regusers = require("./model");
import { NextResponse } from "next/server";
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
ConnectDB();

export async function POST(req) {
  const jss = await req.json();
  console.log("data", jss);

  // return NextResponse.json({ ok: "response from back" });

  try {
    const salt = await bcrypt.genSalt(10);
    const hashpass = await bcrypt.hash(jss.password, salt);

    const newuserdata = await regusers.create({
      username: jss.username,
      email: jss.email,
      password: hashpass,
    });
    console.log("ok", newuserdata);


    return NextResponse.json(
      {
        UserCreatedmessage: "Your account has been created",
        users: newuserdata,
      },
      {
        status: 201,
      }
    );
  } catch (error) {
    console.error("Error creating user:", error);
    return NextResponse.json({ UserCreationError: "Error creating user" });
  }
}
0

There are 0 answers