How to Prevent Caching Errors when using "use cache" in Next.js 15?
Answered
Snowshoe posted this in #help-forum
Original message was deleted.
Answered by Roseate Spoonbill
for queries with errors, I tend to set different revalidation time. I think fetch cache does that by default, but if you use custom cache, try doing something like this:
You can also create custom
Read more here: https://nextjs.org/docs/app/api-reference/functions/cacheLife
// Your code
if(error)
cacheLife('seconds')
else
cacheLife('default')
You can also create custom
cacheLife
profiles in next.config.ts
.Read more here: https://nextjs.org/docs/app/api-reference/functions/cacheLife
8 Replies
Roseate Spoonbill
for queries with errors, I tend to set different revalidation time. I think fetch cache does that by default, but if you use custom cache, try doing something like this:
You can also create custom
Read more here: https://nextjs.org/docs/app/api-reference/functions/cacheLife
// Your code
if(error)
cacheLife('seconds')
else
cacheLife('default')
You can also create custom
cacheLife
profiles in next.config.ts
.Read more here: https://nextjs.org/docs/app/api-reference/functions/cacheLife
Answer
Roseate Spoonbill
you can also set the cache profile inline if this is a one-off handling:
cacheLife({
stale: 0,
revalidate: 0,
expire: 0,
})
Snowshoe
WOW! Such an elegant and simple solution—I love it. I really appreciate your help! ❤️
Roseate Spoonbill
Don't forget to mark the question as answered once you verify that it does exactly what you'd expect 🙂
Snowshoe
how can I do that?
Roseate Spoonbill
Take a look at the first message here -
help-forum
has a bot that contains info about marking something as solution. Simply right-click on the message that solves your problem, go to Apps and use Mark Solution option@Roseate Spoonbill Take a look at the first message here - `help-forum` has a bot that contains info about marking something as solution. Simply right-click on the message that solves your problem, go to Apps and use Mark Solution option
Asian black bear
(Super offtopic: new Tailwind classes not being picked is common issue with Safari and not Turbo specifically. In case you use Safari you might want to test other browsers or disable caching.)