Issues with Vercel Cronjob API Caching Despite Cache-Control Settings
Unanswered
King Shepherd posted this in #help-forum
King ShepherdOP
Hello everyone,
I'm encountering a persistent issue with my API used for a cronjob in a Next.js project deployed on Vercel. Despite implementing various methods to prevent caching, my requests keep hitting the cache, leading to no new requests being processed. Here's a snippet of my API code:
I've set 'Cache-Control' to 'no-store' and tried to ensure there's no caching at different points in my code. However, the Vercel logs still show cache hits:
I'm looking for insights or suggestions on why this might be happening and how to effectively bypass the cache for this cronjob API endpoint.
Thanks in advance for any help!
I'm encountering a persistent issue with my API used for a cronjob in a Next.js project deployed on Vercel. Despite implementing various methods to prevent caching, my requests keep hitting the cache, leading to no new requests being processed. Here's a snippet of my API code:
import {db} from "@/lib/db";
import sanitizeHtml from 'sanitize-html';
async function executeTask() {
console.log('searchTask.findFirst')
const task = await db.searchTask.findFirst({
where: { status: 'PENDING' },
...
const json = await fetchHtmlContent(firstUrl)
console.log(json)
console.log(`Executing task: ${task.name}`);
...
} catch (error) {
console.error(`Error executing task: ${error.message}`);
...
}
}
export async function GET() {
console.log('executeTask')
try {
await executeTask()
return new Response(null, {
status: 200,
headers: {
'Cache-Control': 'no-store'
}
})
} catch (error) {
console.log(error)
return new Response(error, {
status: 500,
headers: {
'Cache-Control': 'no-store'
}
})
}
}
const fetchHtmlContent = async (url) => {
try {
console.log('fetching')
const response = await fetch('url', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ url: url }),
cache: 'no-store',
});
...
return cleanHtml;
} catch (error) {
console.error('Failed to fetch HTML:', error);
}
};I've set 'Cache-Control' to 'no-store' and tried to ensure there's no caching at different points in my code. However, the Vercel logs still show cache hits:
Time: November 21 16:16:35.00 GMT+08:00
Request Path: /api/cron/executeTask
Status Code: 200
Cache: HITI'm looking for insights or suggestions on why this might be happening and how to effectively bypass the cache for this cronjob API endpoint.
Thanks in advance for any help!