Revalidating a GET endpoint in Next

Hey! Does anyone know how to revalidate a cached GET endpoint in Next? With this basic code cached/route.ts
export const GET = async (request: NextRequest) => {
console.log("Getting /cached");
const date = new Date();
return NextResponse.json({ date });
};
export const GET = async (request: NextRequest) => {
console.log("Getting /cached");
const date = new Date();
return NextResponse.json({ date });
};
reval/route.ts
export const dynamic = "force-dynamic";

export const GET = (req: NextRequest) => {
console.log("Revalidating /cached");
revalidatePath("/cached");
revalidatePath("/cached-page");
return NextResponse.json({ done: true });
};
export const dynamic = "force-dynamic";

export const GET = (req: NextRequest) => {
console.log("Revalidating /cached");
revalidatePath("/cached");
revalidatePath("/cached-page");
return NextResponse.json({ done: true });
};
cached-page/page.ts
export default function Page() {
const date = new Date();
return <div>{date.toString()}</div>;
}
export default function Page() {
const date = new Date();
return <div>{date.toString()}</div>;
}
The cached page revalidates fine, however the route handler does not. even calling revalidatePath("/", "layout") which should revalidate everything only does so for the page.
11 Replies
NotRebekahMikaelson™
to make GET request dynamic you need to use cookies() or headers() function see the screennshot according. to nextjs docs
No description
NotRebekahMikaelson™
so for your case - cached/route.ts
import { cookies } from 'next/headers'

export const GET = async (request: NextRequest) => {
console.log("Getting /cached");
cookies()
const date = new Date();
return NextResponse.json({ date });
};
import { cookies } from 'next/headers'

export const GET = async (request: NextRequest) => {
console.log("Getting /cached");
cookies()
const date = new Date();
return NextResponse.json({ date });
};
reval/route.ts - not required cached-page/page.tsx
import { cookies } from 'next/headers'
export default function Page() {
cookies()
const date = new Date();
return <div>{date.toString()}</div>;
}
import { cookies } from 'next/headers'
export default function Page() {
cookies()
const date = new Date();
return <div>{date.toString()}</div>;
}
for the time being i am using cookies() but you can also use headers() as per your requirement ,both are exported from next/headers
janglad
jangladOP9mo ago
yea I know you can make it dynamic like that but I want it to be static, but revalidate when the revalidatePath for it is called so like ISR for pages but seems like that's not possible for routes?
NotRebekahMikaelson™
to be honest i amnot sure but. surely you ca research and let. me. know
janglad
jangladOP9mo ago
yeah I can not find anything about it literally only thing I can find about it is
well you can't use revalidate with route handlers, the api doesn't exist yet
on the next discord also find it so weird that nobody has run into this before, but maybe I just suck at googling
Huilen
Huilen9mo ago
hi
Huilen
Huilen9mo ago
export const revalidate = 3600 // revalidate at most every hour
export const revalidate = 3600 // revalidate at most every hour
NotRebekahMikaelson™
it. works for pages not for routes
janglad
jangladOP9mo ago
It does work for routes, but this is just an interval and not on demand like revalidatepath
NotRebekahMikaelson™
oh i see
Want results from more Discord servers?
Add your server