Search code examples
next.jsserver-side-renderingvercelnext.js13

Can't bypass Vercel edge cache for GET request


I am using the new App Router in NextJS (13.4.6). I have a GET /battles endpoint. Internally, this endpoint queries the database and randomly returns 2 entities. Each time the API is called, the application expects 2 different entities.

Locally everything works fine.

When deploying to Vercel, the GET request seems to get cached, and the same 2 entities are always returned. My route logic does not get executed after the first call.

Looking at the headers, I see: X-Vercel-Cache: HIT. From the Vercel documentation, there are certain criteria where the cached result will be returned from Vercel Edge Network (https://vercel.com/docs/concepts/edge-network/caching#cacheable-response-criteria). In particular:

  • Response does not contain the private, no-cache or no-store directives in the Cache-Control header.

I've tried to bypass the cache by doing the following things:

  • Add 'cache-control': 'private, s-maxage=0, no-store', to my route response.
  • Add export const dynamic = 'force-dynamic'; to my Page component.
  • Add cache: 'no-store', to my client fetch request.

None of these seem to be working and I'm out of ideas. A hacky fix is to change to a POST request (which solves the issue), but this is not ideal.


Solution

  • Read Route Segment Config

    The Route Segment options allows you configure the behavior of a Page, Layout, or Route Handler by directly exporting the following variables:

    // copy paste did not paste in a table format enter image description here

    I think you need to set

      // default false
      export const revalidate=0 // or low number