Updated data not being fetched in production.

Hey guys so i just made a web app which uses prisma / planetscale and tanstack query. I have been trying to fetch data from the code below
const { data, refetch, isRefetching } = useQuery({
queryKey: ["normal_fetch"],
queryFn: async () => {
const { data } = await axios.get("/api/votes/fetchVote");
return data as VoteProps[];
},
onError: (err) => {
if (err instanceof AxiosError) {
if (err.response?.status === 401) {
return console.log("errror");
}
}
},
});
const { data, refetch, isRefetching } = useQuery({
queryKey: ["normal_fetch"],
queryFn: async () => {
const { data } = await axios.get("/api/votes/fetchVote");
return data as VoteProps[];
},
onError: (err) => {
if (err instanceof AxiosError) {
if (err.response?.status === 401) {
return console.log("errror");
}
}
},
});
It works perfectly fine in the local enviroment but the deployed version of the app doesn't fetch the updated data from database at all what might be the problem here ?
6 Replies
swikarrr
swikarrrOP2y ago
import { db } from "@/lib/db";
import { z } from "zod";

export async function GET(req: Request) {
try {
const votes = await db.vote.findMany();
return new Response(JSON.stringify(votes));
} catch (error) {
if (error instanceof z.ZodError) {
return new Response("Invalid request data passed", { status: 422 });
}

return new Response("Could not fetch the data.", {
status: 500,
});
}
}
import { db } from "@/lib/db";
import { z } from "zod";

export async function GET(req: Request) {
try {
const votes = await db.vote.findMany();
return new Response(JSON.stringify(votes));
} catch (error) {
if (error instanceof z.ZodError) {
return new Response("Invalid request data passed", { status: 422 });
}

return new Response("Could not fetch the data.", {
status: 500,
});
}
}
this one is route
Luc Ledo
Luc Ledo2y ago
https://nextjs.org/docs/app/building-your-application/routing/router-handlers#behavior Try with NextResponse, your route is probably being statically evaluated.
swikarrr
swikarrrOP2y ago
thank you , but i got it it was happening because of caching
Luc Ledo
Luc Ledo2y ago
how did you fix it?
swikarrr
swikarrrOP2y ago
i just needed to revalidate the data fetching apparently it was calling the api for 1 time and then using that throughout so i added export const revalidate = 0; at top my route
Luc Ledo
Luc Ledo2y ago
Yeah, it was statically evaluated, you just forced to it to be dynamic. Usually, you wouldn't need to do that since using the Request object should automatically make the route dynamic by default.
Want results from more Discord servers?
Add your server