We are trying to add Meta information based on the data fetched from an API. We are statically exporting pages using export. It works perfectly on the development but when I try to make a build of it (next build with export enabled) this function is invoked multiple times and hence the API throws a 429 time-out error.
Following is the function doing the business logic:
export async function generateMetadata(): Promise<Metadata> {
const portal = await GetPortalTheme().then((res: any) => {
if (res.data) {
return res.data.portal;
}
});
const setting = JSON.parse(portal.settings || "{}");
return {
title: portal.name || "portal",
description: "Innovate your out bound call",
icons: {
icon: setting.logo?.url,
},
};
}
What it should do?
This API should be called once and the MetaData should be set for all the pages.
So, to come up with a solution to solve this problem we have implemented a strategy that stores values locally, and upon fetching from API, the data is stored in the local variable. Then we placed a condition that only if that value does not exist, only then call the API otherwise do not call it.
Here is the approach we followed:
Please let me know if we can improve this code or if you have any suggestions.