NextJS API Mongodb keep fetching old data

Title: Next.js website hosted on Netlify not displaying updated data from MongoDB API

I have fixed the problem by including the revalidation inside my routes.

import db from "@/utils/dbConnect";

export async function GET() {
    // Setting up the full connection string
    const collectionName = "projects";

    const collection = db.collection(collectionName);

    // Finding all documents in the collection
    const projects = await collection.find({}).toArray();

    // Returning the skills
    return new Response(JSON.stringify(projects), {
        headers: { "content-type": "application/json" },
    });
}

// revalidate every 10 seconds
export const revalidate = 10;

Hello everyone,

I am facing an issue with my Next.js website hosted on Netlify. Locally, everything is working perfectly fine. However, after deploying the website on Netlify using the Next.js runtime, the displayed data from my MongoDB API is always outdated. It takes a long time for the website to reflect the updated data from the database.

EDIT: I tried running “npm run build && npm run start” and I had the same problem as the deployed site, so this issues only happens on the build.

To provide assistance, please refer to the following details:

  1. Netlify site name: https://aymvn.netlify.app/
  2. Custom domain: https://aymvn.tech
  3. Github repository: GitHub - itzAymvn/aymvn.tech: The portfolio showcasing some of my projects and my skills
  4. Build log and build settings screenshot:

Furthermore, here is an overview of my website’s setup:

  • Technology stack: Next.js 13, MongoDB
  • The website fetches data from a MongoDB API and displays it using the fetch function.
  • Locally, the data updates properly when changes occur in the database by simply refreshing the page.

I would greatly appreciate any assistance in resolving this issue and ensuring that the deployed website on Netlify reflects the most up-to-date data from the MongoDB API in a timely manner.

Thank you in advance for your help!

Glad you fixed your problem and thanks for sharing how you resolved your problem.

thanks for coming back @aymvn and sharing your solution.