Disable server-side cache of serverless functions
Answered
American Shorthair posted this in #help-forum
American ShorthairOP
I have a nextjs app using nextjs 13 with the app router. One endpoint returns a randomInt:
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?
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) });
}3 Replies
Using the dynamic ='force-dynamic'orrevalidate = 0route 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!