Route handler not keeping fetch cache
Unanswered
Scottish Fold posted this in #help-forum
Scottish FoldOP
I created a simple api to provide price of a cryptocurrency. It consumes from coinmarketcap and it is configured to cache for 5 minutes.
So, even with constant access, it should not consume the coinmarketcap api more than 8928 times per month.
However, it always take ~1 week to consume 10K requests. It seems the cache is not working as expected.
Here is the function the requests from coinmarketcap and cache for 5 minutes https://github.com/nanodrop/nanodrop.io/blob/main/src/services/coinmarketcap.ts
Here is the route:
https://github.com/nanodrop/nanodrop.io/blob/main/src/app/api/price/route.ts
Am I doing something wrong? Help!
So, even with constant access, it should not consume the coinmarketcap api more than 8928 times per month.
However, it always take ~1 week to consume 10K requests. It seems the cache is not working as expected.
Here is the function the requests from coinmarketcap and cache for 5 minutes https://github.com/nanodrop/nanodrop.io/blob/main/src/services/coinmarketcap.ts
Here is the route:
https://github.com/nanodrop/nanodrop.io/blob/main/src/app/api/price/route.ts
Am I doing something wrong? Help!
19 Replies
@B33fb0n3 This is probably related to the "Cache api requests server side" question too.
Sun bear
Maybe it has to do with:
"In Server Actions, fetch requests are not cached (defaults cache: no-store)."
https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating
"In Server Actions, fetch requests are not cached (defaults cache: no-store)."
https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating
Got it
@Scottish Fold do you have full url logging enables on your next config
Scottish FoldOP
@Arinji it seems locally it's keeping the cache. I will make some tests on production
Scottish FoldOP
clearly the cache only works locally. The code is the same in production
Also you don't have to ping me thrice
@Arinji Also you don't have to ping me thrice
Scottish FoldOP
I ping you thrice because I deleted 2 previous. You don't need to be so rude. Also your response has nothing to do with my question. I've already tested logging and told you the cache is not working in production
@Scottish Fold I ping you thrice because I deleted 2 previous. You don't need to be so rude. Also your response has nothing to do with my question. I've already tested logging and told you the cache is not working in production
What you showed me was just how long it takes for a response? If you don't want to troubleshoot basic stuff.. then it would be best for you to wait for someone else to help.
@Arinji What you showed me was just how long it takes for a response? If you don't want to troubleshoot basic stuff.. then it would be best for you to wait for someone else to help.
Scottish FoldOP
sorry, I tought it was the logs from the fullUrl config. But yeah, it's not, it's not including "HIT" "MISSING". Not sure why
I'm having the same issue, but it seems that this hasn't been answered yet
I hit the ratelimit on the API I'm using, but that shouldn't be happening (all I'm doing is refreshing the page and it keeps making a new request)
export async function GET(request: Request) {
const { searchParams } = new URL(request.url);
const query = searchParams.get("query");
if (query === null) {
return new Response("Error: query not provided", { status: 400 });
}
const url = `https://financialmodelingprep.com/api/v3/search?query=${query.toUpperCase()}&limit=10&apikey=${
process.env.FINANCIAL_MODELING_PREP_KEY
}`;
try {
const res = await fetch(url, {
headers: {
"Content-Type": "application/json",
},
});
const data = await res.json();
return Response.json(
{ data },
{
status: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
},
}
);
} catch (error) {
console.error(error)
}
}even worse actually, it's making a request every time I edit the code, no wonder why I hit the ratelimit so fast
In the docs, it says route handlers should be cached, so why isn't it working?
In the docs, it says route handlers should be cached, so why isn't it working?
you are using search params from request, this will make it not cache.
https://nextjs.org/docs/app/building-your-application/routing/route-handlers#opting-out-of-caching
Just wrap your
in https://nextjs.org/docs/app/api-reference/functions/unstable_cache
make sure to go through the keyparts and add the query to it, or else nextjs wont give each query a specific cache.
https://nextjs.org/docs/app/building-your-application/routing/route-handlers#opting-out-of-caching
Just wrap your
const url = `https://financialmodelingprep.com/api/v3/search?query=${query.toUpperCase()}&limit=10&apikey=${
process.env.FINANCIAL_MODELING_PREP_KEY
}`;in https://nextjs.org/docs/app/api-reference/functions/unstable_cache
make sure to go through the keyparts and add the query to it, or else nextjs wont give each query a specific cache.
@MooKorea
@Scottish Fold regarding your thing, i would just let the function not cache anything, and cache calling the function itself.
So make the function have no cache when it calls coinmarketcap. And wrap this https://github.com/nanodrop/nanodrop.io/blob/main/src/app/api/price/route.ts#L9 with unstable cache, and give it a revalidate of 5 mins.
So make the function have no cache when it calls coinmarketcap. And wrap this https://github.com/nanodrop/nanodrop.io/blob/main/src/app/api/price/route.ts#L9 with unstable cache, and give it a revalidate of 5 mins.