Hi Parth,
Thank you for your quick response.
We went through the CSV file, and this is the information that we saw earlier in our Builder.io dashboard, but unfortunately, we saw this sudden sharp rise in bandwidth consumption for the “…/news” api call.
Nothing has changed in our codebase so far related to this section, and no other section using this.
//builder sdk functions used to fetch news data
export async function fetchFilteredModelData(
modelName: string,
query: Record<string, any>,
): Promise<any> {
try {
return await builder.getAll(modelName, {
query,
cachebust: true,
noTraverse: false,
});
} catch (error) {
console.error(“Error fetching filtered data:”, error);
return [];
}
}
export async function fetchAllBuilderModelData(
modelName: string,
): Promise<any> {
try {
return await builder.getAll(modelName, { cachebust: true });
} catch (error) {
console.error(“Error fetching all model data:”, error);
return [];
}
}
export async function fetchBuilderDataById(
modelName: string,
id: string,
): Promise {
try {
return await builder.get(modelName, {
query: {
id: id,
},
cachebust: true,
});
} catch (error) {
console.error(“Error fetching data by ID:”, error);
return null;
}
}
export async function fetchNewsDetailsbyId(id: string) {
const newsDetails = await fetchBuilderDataById(“news”, id);
if (!newsDetails) {
return null;
}
return newsDetails;
}
export const fetchNewsData = async (
slug: string,
currentPage: number,
orderValue: 1 | -1,
): Promise<LatestNewsItem> => {
try {
const entry = await fetchFilteredModelData(“news”, {});
const newsList = entry.filter((eachNews) => {
return eachNews.data.categories
? eachNews.data.categories.some((eachCategory) => {
return slug === createNameSlug(eachCategory.category);
})
: false;
});
newsList.sort((a: any, b: any) => {
const dateA = new Date(a.data.date).getTime();
const dateB = new Date(b.data.date).getTime();
return (dateB - dateA) * orderValue;
});
const endIndex = currentPage * 3;
const slicedData = newsList.slice(0, endIndex);
return slicedData.map((item: any) => ({
...item.data,
id: item.id,
}));
} catch (error) {
console.error(“Error fetching and sorting news items by slug:”, error);
return ;
}
};
export const fetchAllNewsData = async (
currentPage: number,
orderValue: 1 | -1,
): Promise<LatestNewsItem> => {
try {
const entry = await fetchAllBuilderModelData(“news”);
entry.sort((a: any, b: any) => {
const dateA = new Date(a.data.date).getTime();
const dateB = new Date(b.data.date).getTime();
return (dateB - dateA) * orderValue;
});
const endIndex = currentPage * 3;
const slicedData = entry.slice(0, endIndex);
return slicedData.map((item: any) => ({
...item.data,
id: item.id,
}));
} catch (error) {
console.error(“Error fetching all news data:”, error);
return ;
}
};
//functions being used in nextjs unstable_cache to cache the data on server side
export const getCachedNewsBuilderDataById = unstable_cache(
async (id) => {
const data = await fetchNewsDetailsbyId(id);
return data;
},
[“news-details”],
{
tags: [“news-details”],
revalidate: SSG_REVALIDATE_INTERVAL,
},
);
export const getCachedAllNewsData = unstable_cache(
async (currentPage: number, orderValue: 1 | -1) => {
const data = await fetchAllNewsData(currentPage, orderValue);
return data;
},
[“all-news-details-key”],
{
tags: [“news-details”, “all-news-list”],
revalidate: SSG_REVALIDATE_INTERVAL,
},
);
//calling cached functions in page.tsx files
const newsItems = await getCachedAllNewsData(currentPage, orderValue);
const entry = await getCachedNewsBuilderDataById(id);
If you need more code snippets from me, I can share them, but currently our approach is that we are using cached data of the API from nextJS applicatio,n and this is invalidated after 4 hours. This logic remained intact right from the beginning and this spike we saw happened since end of last month. This is concerning.
Would be helpful if you could share more details on the requests made to this API, narrow it down and the root cause of this spike and then possible solution to avoid this.