Next.js Discord

Discord Forum

GET request from API returns old data

Answered
Skipjack tuna posted this in #help-forum
Open in Discord
Skipjack tunaOP
I'm calling /api/posts route,
import { NextResponse } from "next/server";

import prisma from "@/lib/prismadb";

export async function GET(req: Request) {
  try {
    const posts = await prisma.post.findMany({
      include: {
        user: true,
        comments: true,
      },
      orderBy: {
        createdAt: "desc",
      },
    });
    return NextResponse.json(posts);
  } catch (error: any) {
    console.log(error);
    return new NextResponse(
      JSON.stringify({ status: "fail", message: error.message }),
      { status: 400 }
    );
  }
}

This works fine on my local environment, it updates on each change, but when I deploy on the Vercel server, it does not update the data, even on refresh and would send the same data at the time of building.
Answered by joulev
export const dynamic = force-dynamic
View full answer

31 Replies

Mugger Crocodile
There is a caching issue; I have also encountered it before. I have updated the background, but I am still receiving the old data. Have you tried revalidation?
Mugger Crocodile
hmm I also tried that, but it didn't work.
@Mugger Crocodile hmm I also tried that, but it didn't work.
Skipjack tunaOP
Did you fix it?
Mugger Crocodile
no am also getting old data
Skipjack tunaOP
Should we report to vercel?
Mugger Crocodile
Maybe the issue is on our side. If I manage to fix it, I will update you here.
@Mugger Crocodile have you seen this https://github.com/vercel/next.js/discussions/42290
Skipjack tunaOP
Try deploying on netlify, the issue only exists on vercel
Mugger Crocodile
alright am going to see it !
@Mugger Crocodile alright am going to see it !
Skipjack tunaOP
Let me know
Answer
This is not a bug btw, that’s how it is designed
Static by default
@joulev export const dynamic = force-dynamic
Skipjack tunaOP
In page.tsx or route.ts?
@Skipjack tuna In page.tsx or route.ts?
in route.ts
Skipjack tunaOP
I'll try
@joulev export const dynamic = force-dynamic
Skipjack tunaOP
still geting the cached api response
@Skipjack tuna still geting the cached api response
Give me a repro repo
the /api/posts route is not working
as intended
works fine on netlify tho, only Vercel deployment is causing issue
@Skipjack tuna https://github.com/itexpert120/twitter-clone
I said “force-dynamic” not “auto”
They are not the same
auto is the default value, you may as well just dont set it if you use auto
Ratonero Murciano de Huerta
adding export const revalidate = 60; worked for me for getting new data in a local rails api
no idea how or why this works but it was from a previous help-forum post
@joulev auto is the default value, you may as well just dont set it if you use auto
Skipjack tunaOP
THANKS, it works now
@joulev export const dynamic = force-dynamic
Mugger Crocodile
it worked also for me , thanks