Next.js Discord

Discord Forum

generateStaticParams() returns undefined from different slugs in every build

Unanswered
Asian paper wasp posted this in #help-forum
Open in Discord
Asian paper waspOP
I am generating static params for my pages but I always get error from different page numbers (in the screenshot, it is /list/characters/26, so the page number giving error is 26, but it always changes in every build). Is there a way to handle errors in generateStaticParams()?

2 Replies

Asian paper waspOP
export async function generateStaticParams() {
    // Getting discoverData used for main page, then getting display data from getDisplayData, to fetch entities list data in the following function (allPagesData).
    const entityDisplayDataArray = discoverData.map((item) =>
        getDisplayData(item.search as EntityType)
    );

    // Fetching every entity type's page 1 data.
    const allPagesData = await Promise.all(
        entityDisplayDataArray.map(async (item) => getListPage(item!.fetchKeyword, "1"))
    );

    // Looping over every entity's page data, and getting the total counts of each entity type.
    const totalEntities = allPagesData.map(
        (item, idx) => item[entityDisplayDataArray[idx]!.totalKeyword]
    );

    // Getting page counts for each entity
    const totalPages = totalEntities.map((item) => Math.ceil(item / pageSize));

    function getStaticParams() {
        let staticParams = [];

        for (let et = 0; et < discoverData.length; et++) {
            const entityType = discoverData[et].search;

            for (let pn = 1; pn <= totalPages[et]; pn++) {
                staticParams.push({ entityType: entityType, pageNumber: pn.toString() });
            }
        }

        return staticParams;
    }

    return getStaticParams();
}
Asian paper waspOP
Building the project locally doesn't thow any errors, but building it on Vercel does.