How do I disable cache on GET api route?
Unanswered
Cornish Rex posted this in #help-forum
Cornish RexOP
I have an API route, with a GET handler. When I deploy the app to Vercel, I can't trigger subsequent emails because the response appears to be cached. What am I doing wrong in the
route.tsimport { Resend } from "resend";
export const runtime = "edge";
export const dynamic = "force-dynamic";
const noCacheHeaders = {
"Cache-Control": "no-store, max-age=0",
"Surrogate-Control": "no-store",
"Vercel-CDN-Cache-Control": "no-store",
};
export async function GET() {
const apiKey = process.env.RESEND_API_KEY as string;
const resend = new Resend(apiKey);
const currentDate = new Date().toISOString().split("T")[0];
const defaultSubject = `Daily Email ${currentDate}`;
if (!resend) {
// handle error
}
const to = "matt@mattwood.co";
const subject = defaultSubject;
const message = "Here are your links for today";
try {
const data = await resend.emails.send({
from: "insite.wiki@bouqt.xyz",
to,
subject,
html: `
<p>Here are your links for ${currentDate}:</p>
<ul>
<li><a href="https://insite.wiki">Insite Wiki</a></li>
<li><a href="https://mattwood.co">Matt Wood</a></li>
</ul>
<p>${message}</p>
`,
});
if (data.error) {
// handler error
}
return new Response(
JSON.stringify({
message: "Email sent",
date_sent: currentDate,
...data.data,
}),
{
status: 201,
headers: {
"Content-Type": "application/json",
...noCacheHeaders,
},
}
);
} catch (error) {
// handler error
}
}6 Replies
GET api route is cached in production by default
You can try POST instead
Broad-snouted Caiman
In nextjs with my personal experience i use the revalidation export const revalidate variable and then assigned a value to it on the server.
this saved me in one of my projects.
so you can assign any value to it including
this saved me in one of my projects.
export const revalidate = 10so you can assign any value to it including
0 since it's read in seconds@Cornish Rex
Cornish RexOP
Great, thank you I will check it out