How to avoid caching for specific Nextjs api route?

Viewed 942

I have nextjs api route /api/auth/signout, it basically just clears the specific cookie and send json back.

The problem is, when I deploy project in Vercel, this particular API working first time properly(cookie cleared and giving 200 response) but later it's not working (cookie not cleared and giving 304 response).

I want to know, is there any way to avoid cache only for this route?

What's the best possible way to fix this problem?

2 Answers

Added this config on next.config.js

async headers() {
    return [
      {
        source: '/api/<route-name>',
        headers: [
          {
            key: 'Cache-Control',
            value: 'no-store, max-age=0',
          },
        ],
      },
    ];
  },
Related