Search code examples
cachingnext.jsroutesvercel

How to avoid caching for specific Nextjs API route?


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?


Solution

  • Added this config on next.config.js

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