Next.js Discord

Discord Forum

I always get cached responses in production

Unanswered
Perro de Presa Mallorquin posted this in #help-forum
Open in Discord
Perro de Presa MallorquinOP
Reposting this from Vercel discussions:

I am building a website with Astro in which I created an API route. I always get a cached response from this route and I would like to have more control over this behaviour (this is only an issue in production, not when developing). (https://daniell.dev/)

From my endpoint, I am sending:
return new Response(
  JSON.stringify({
    currentlyPlaying:
      currentlyPlayingTrack && formatTrack(currentlyPlayingTrack),
    topTracks: userTop.items.map(formatTrack),
  } satisfies APIResponse),
  {
    headers: {
      "Cache-Control": "public, s-maxage=60, stale-while-revalidate=30",
    },
  },
);

I request this endpoint like:
import { nanoquery } from "@nanostores/query";

export const [createFetcherStore, createMutatorStore] = nanoquery({
  fetcher: (...keys: Array<string | number | true>) =>
    fetch(keys.join("")).then((r) => r.json()),
});

export const $spotifyData = createFetcherStore<APIResponse>(["/api/spotify"]);

I have also tried to add a vercel.json file containing:
{
  "headers": [
    {
      "source": "/api/spotify",
      "headers": [
        {
          "key": "Cache-Control",
          "value": "public, s-maxage=60, stale-while-revalidate=30"
        }
      ]
    }
  ]
}

5 Replies

Perro de Presa MallorquinOP
I was doubting whether it's an Astro issue or more Vercel related
Because if it's Astro related I couldn't imagine how it'd be solved in Next.js. I would assume I run into the same issue there
@Perro de Presa Mallorquin I was doubting whether it's an Astro issue or more Vercel related
try running the astro app in prod mode locally. if the issue is still there, it is likely a code bug you need to fix (and the astro server will be helpful).

if the issue is not there, it is indeed a vercel bug but this server won't be of much help since, as the name suggests, this is a nextjs server – the chance of someone using astro and having the same issue and knowing the solution is low.

if it is a vercel bug you can contact support if you have a paid plan with vercel. if you are on the hobby plan, github discussions is the only place to go.
Perro de Presa MallorquinOP
Alright ty