Next.js Discord

Discord Forum

Disable server-side cache of serverless functions

Answered
American Shorthair posted this in #help-forum
Open in Discord
American ShorthairOP
I have a nextjs app using nextjs 13 with the app router. One endpoint returns a randomInt:
import { randomInt } from "crypto";
import { NextResponse } from "next/server";

export async function GET() {
  return NextResponse.json({ revenue: randomInt(100) });
}


In local, every time I load this endpoint, I get a different number. But when in prod on vercel I always get the same number like if it was cached. How can I disable it?
Answered by riský
so this should work:
import { randomInt } from "crypto";
import { NextResponse } from "next/server";

export const dynamic = 'force-dynamic' 

export async function GET() {
  return NextResponse.json({ revenue: randomInt(100) });
}
View full answer

3 Replies

Using the dynamic = 'force-dynamic'or revalidate = 0 route segment config options: This will skip the Full Route Cache and the Data Cache. Meaning components will be rendered and data fetched on every incoming request to the server. The Router Cache will still apply as it's a client-side cache. [[source](https://nextjs.org/docs/app/building-your-application/caching#opting-out-2)]
so this should work:
import { randomInt } from "crypto";
import { NextResponse } from "next/server";

export const dynamic = 'force-dynamic' 

export async function GET() {
  return NextResponse.json({ revenue: randomInt(100) });
}
Answer
American ShorthairOP
Thank you sir, it worked!