1

I have an API route that generates data in my DataBase, this API route is only called in my CRON job set in Vercel.

However, every time I build the project, new data show in the database. I do believe it is because of the Nature of NextJS to pre-execute an endpoint so it will have on cache, but how do I prevent adding data in my Database?

enter image description here enter image description here

The code is simple: https://github.com/guifeliper/dart-throwing-monkey-crypto/blob/main/apps/monkey-crypto/app/api/generateTokens/route.ts

export const revalidate = 60 * 60 * 24 * 6; // every 6 days
export async function GET(request: Request) {
  const url =
    `${process.env.COINMARKETCAP_URL}/v1/cryptocurrency/listings/latest?limit=100` ??
    "";
  var options = {
    headers: {
      "X-CMC_PRO_API_KEY": process.env.COINMARKETCAP_API ?? "",
    },
    next: { revalidate: 60 * 60 * 24 * 6 },
  };

  const res = await fetch(url, options);
  const data = await res.json();

  const tokensList: Tokens[] =
    data?.data?.map((token: any) => {
      return {
        name: token.name,
        symbol: token.symbol,
        priceAtDrawn: token.quote["USD"].price,
        priceAtContest: 0,
        category: "Top-100",
        timeframe: getYearWeekString(),
      };
    }) ?? [];

  const BTCBenchmark = tokensList
    .filter((token) => token.symbol === "BTC")
    .map((token) => ({ ...token, category: "BTC-benchmark" }));

  if (tokensList?.length == 0) {
    return NextResponse.json(
      { message: "Error on token list" },
      { status: 500 }
    );
  }
  const selectedTokens = selectTokens(tokensList, 10);
  return await addTokensDrawn([...selectedTokens, ...BTCBenchmark]);
}

async function addTokensDrawn(data: any) {
  try {
    await prisma.tokenDrawn.createMany({ data });
    return NextResponse.json({ message: "Add Successfully" }, { status: 200 });
  } catch (error) {
    console.error("Request error", error);
    return NextResponse.json({ message: "Error on add" }, { status: 500 });
  }
}
  • Your whole question (including any necessary code) has to be **in** your question, not just linked. Three reasons: People shouldn't have to go off-site to help you; some sites are blocked for some users; and links rot, making the question and its answers useless to people in the future. Please put a [mcve] **in** the question, or if code isn't necessary, don't include a link to it. – T.J. Crowder Jun 02 '23 at 15:18
  • 2
    Does [this section of the next js docs](https://nextjs.org/learn/basics/data-fetching) answer your question? You basically need to make sure it isn't doing SSG but SSR on that route I think – Zachiah Jun 02 '23 at 15:43
  • 1
    thank you @Zachiah, I am investigating this. It does make sense what you said. – Guilherme Felipe Reis Jun 02 '23 at 20:12

1 Answers1

1

use export const dynamic = 'force-dynamic'; atop your function definition.

afaik next.js has some detection in place to check if it can SSR some functions, like checking if you're using headers or cookies, but if these calls are not detected in the immediate surroundings after the function definition there are problems, so putting force-dynamic bypasses the check.

https://github.com/vercel/next.js/issues/49441

John Smith
  • 1,726
  • 2
  • 18
  • 30