fetch API is not fetching for the latest data
Unanswered
Exotic Shorthair posted this in #help-forum
Exotic ShorthairOP
I have an endpoint that gets the last updated timestamp from my database. I confirmed the logic is working for the SQL query. However, I noticed when the last updated time in the database changes, and I go to refresh my page, the latest data is not being fetched. I also don't see the endpoint fetch being called in my server logs. The only time I notice the timestamp updates, is when I have to push new code to vercel and once the deployment is done, then the new timestamp is showing. Here is the code function snippet that is called on my home page.
here is a link to my code if anyone wants to get more context: https://github.com/jdouglass/collection-coffee/blob/main/web/collection-coffee/src/app/page.tsx
async function getLastUpdatedDetails(): Promise<ILastUpdatedResponse> {
try {
const res = await fetch(`${process.env.API_BASE_URL}/api/v1/last-updated`);
if (!res.ok) {
throw new Error("Failed to fetch products");
}
return res.json();
} catch (error) {
console.error(error);
return {
lastUpdatedDateTime: new Date(),
isScraperRunning: false,
};
}
}here is a link to my code if anyone wants to get more context: https://github.com/jdouglass/collection-coffee/blob/main/web/collection-coffee/src/app/page.tsx
18 Replies
@Exotic Shorthair I have an endpoint that gets the last updated timestamp from my database. I confirmed the logic is working for the SQL query. However, I noticed when the last updated time in the database changes, and I go to refresh my page, the latest data is not being fetched. I also don't see the endpoint fetch being called in my server logs. The only time I notice the timestamp updates, is when I have to push new code to vercel and once the deployment is done, then the new timestamp is showing. Here is the code function snippet that is called on my home page.
async function getLastUpdatedDetails(): Promise<ILastUpdatedResponse> {
try {
const res = await fetch(`${process.env.API_BASE_URL}/api/v1/last-updated`);
if (!res.ok) {
throw new Error("Failed to fetch products");
}
return res.json();
} catch (error) {
console.error(error);
return {
lastUpdatedDateTime: new Date(),
isScraperRunning: false,
};
}
}
here is a link to my code if anyone wants to get more context: https://github.com/jdouglass/collection-coffee/blob/main/web/collection-coffee/src/app/page.tsx
i think you need to opt out of fetch cache https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#individual-fetch-requests
Exotic ShorthairOP
for some reason it seems to work locally but not when i try it on my instance that's hosted on vercel
so adding the config to fetch didn't work?
Exotic ShorthairOP
no, for some reason it didn't
as you can see on my website (https://collection.coffee/) the last updated date is stuck at yesterday's date when I last deployed my application to production. however, in my db there are more recent timestamps that should be getting pulled
actually it looks like the reason is that when i built my application, the endpoints were rendered statically 🤔
im referring to last-updated, reference data, and system status
@Exotic Shorthair as you can see on my website (https://collection.coffee/) the last updated date is stuck at yesterday's date when I last deployed my application to production. however, in my db there are more recent timestamps that should be getting pulled
have you disable the data cache?
fetch("", { cache: "no-store" })@Exotic Shorthair actually it looks like the reason is that when i built my application, the endpoints were rendered statically 🤔
GET endpoint is static by default
@Ray GET endpoint is static by default
Exotic ShorthairOP
so in that case, it would fetch on build time?
Exotic ShorthairOP
ah i think that's the issue
you could use route segment config to set it dynamic
export const dynamic = 'force-dynamic'Exotic ShorthairOP
so i would need to add that to the top of all of my endpoints that I want to be dynamically rendered?
Exotic ShorthairOP
nice it seems to work now 🙂 i deployed the changes and then tweaked a time in my prod database as a test and now it updated as i would have expected
thanks for the help on this!
no prob