#How do I disable cache on GET api route?

8 messages · Page 1 of 1 (latest)

frozen jewel
#

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.ts


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 = "[email protected]";
  const subject = defaultSubject;
  const message = "Here are your links for today";

  try {
    const data = await resend.emails.send({
      from: "[email protected]",
      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
  }
}
dark gulchBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

frozen jewel
#

How do I disable cache on GET api route?

obsidian smelt
#

GET api route is cached in production by default

#

You can try POST instead

pale bloom
#

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.

export const revalidate = 10

so you can assign any value to it including 0 since it's read in seconds

obsidian smelt
#

@frozen jewel