Next.js Discord

Discord Forum

Preventing Cache on Error Responses from API Calls

Unanswered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
Hey everyone,

I’m facing an issue with my app related to external API calls in server components. The problem is that the API sometimes responds with a 200 status code, but the response contains an error message (example: { error: "error-message" }). I’m handling these errors on the client, but the problem is that these responses are getting cached.

how can I prevent caching when an error is received, even if the response has a 200 status code? I want to ensure that a new fetch call is made on reload instead of using the cached response.

And I was wondering, if the API were to send a 500 status code, I assume the response wouldn’t be cached. Is that correct?

Thanks so much for your help!

4 Replies

@Masai Lion in next.js by default GET route is cached by default unless you use cookies or headers
why don't you show me your code?
noStore from unstable_cache if you want your api returns fresh response instead of cached one