Next.js Discord

Discord Forum

Value not updating in production but work fine in local server

Answered
Rex posted this in #help-forum
Open in Discord
RexOP
I using Next js, prisma and swr to fetch data from the database. The code is working fine in the dev environment but when I push the chanegs to vercel I don't get the latest value on value change in for the current stock.
Here are code snippets
**My api call on react component**
  const { data: currentStockRecord } = useSWR(
    "/api/dashboard/current-stock",
    fetcher,
  );

**api **
import { prisma } from "@/db/db";
import { currentUtcDate } from "@/lib/currentUtcDate";
import { endOfMonth, subMonths } from "date-fns";
import { NextRequest, NextResponse } from "next/server";

export async function GET(req: NextRequest) {
  const currentCountRecord = await prisma.totalStockCount.findMany({
    orderBy: {
      date: "desc",
    },
    take: 1,
  });
  console.log("currentCountRecord: ", currentCountRecord);
  return NextResponse.json(currentCountRecord, { status: 200 });
}
**in issue componet tried manually updating data**
  const onSubmit = async (values: z.infer<typeof issueFormSchema>) => {
    console.log("VALUES: ", values);
    try {
      await axios.post(`/api/products/${id}/issue-product`, { ...values, id });
      mutate(`/api/categories/${categoryId}`);
      mutate(`/api/dashboard/current-stock`);

      setOpen((prev) => false);
      toast.success("Item issued Successfully");
    } catch (error) {
      console.log("error:", error);
      // @ts-ignore
      toast.error(error?.response?.data);
    }
  };

After issuing item post api call I tried mutating the daat for the current-stock but still it does not work in production. The value is not updated. Note it works fine on local server.
The value is updated in the db I checked through the prisma studio
Answered by joulev
Add export const revalidate = 0 to the route handler file
View full answer

8 Replies

@Rex I using Next js, prisma and swr to fetch data from the database. The code is working fine in the dev environment but when I push the chanegs to vercel I don't get the latest value on value change in for the current stock. Here are code snippets js **My api call on react component** const { data: currentStockRecord } = useSWR( "/api/dashboard/current-stock", fetcher, ); **api ** import { prisma } from "@/db/db"; import { currentUtcDate } from "@/lib/currentUtcDate"; import { endOfMonth, subMonths } from "date-fns"; import { NextRequest, NextResponse } from "next/server"; export async function GET(req: NextRequest) { const currentCountRecord = await prisma.totalStockCount.findMany({ orderBy: { date: "desc", }, take: 1, }); console.log("currentCountRecord: ", currentCountRecord); return NextResponse.json(currentCountRecord, { status: 200 }); } **in issue componet tried manually updating data** const onSubmit = async (values: z.infer<typeof issueFormSchema>) => { console.log("VALUES: ", values); try { await axios.post(`/api/products/${id}/issue-product`, { ...values, id }); mutate(`/api/categories/${categoryId}`); mutate(`/api/dashboard/current-stock`); setOpen((prev) => false); toast.success("Item issued Successfully"); } catch (error) { console.log("error:", error); // @ts-ignore toast.error(error?.response?.data); } }; After issuing item post api call I tried mutating the daat for the current-stock but still it does not work in production. The value is not updated. Note it works fine on local server. The value is updated in the db I checked through the prisma studio
Add export const revalidate = 0 to the route handler file
Answer
@joulev Add `export const revalidate = 0` to the route handler file
RexOP
yes this works on vercel
can you please explain what was causing the issue
RexOP
I checked the docs but don't understand what do you mean by 'statically generated routes'
Thanks for helping
If i had changed the export async function GET(req: NextRequest) to export async function GET(req: Request) apparently that also has worked
@Rex I checked the docs but don't understand what do you mean by 'statically generated routes'
It means it’s run at build time and not rerun again, so all requests to the route will get the same cached result.

No using a different type (Request instead of NextRequest) doesn’t help. You must configure the route to be dynamically generated, and the export revalidate above is one of the ways to do it.