Set a limit when statically outputting individual pages from Contentful
This year, the number of articles on this site passed 100. That was a happy milestone, but at the same time an unexpected problem appeared. Some articles became 404 Not Found.
This site uses Next.js export to statically output HTML. Article lists were displayed correctly, but some individual article pages were not generated at build time. That means the problem was in individual article generation. Currently, article lists are fetched with pagination, so limit is set to 5 for the number of articles displayed per page.
On the other hand, I had not specified limit for the getEntries() function used by generateStaticParams() when outputting individual articles. I had intended to fetch all pages somehow and build them, but I had not noticed that the default was set to 100.
Thinking about it carefully, an API that retrieves all entries with no default limit would be dangerous, but when the site launched I had not thought that far. According to the documentation:
> Note: The maximum number of entries returned by the API is 1000. The API will throw a BadRequestError for values higher than 1000 and values other than an integer. The default number of entries returned by the API is 100.
So 100 is the default, and 1000 is the maximum. If the count exceeds 1000, some extra handling will be needed, but I do not expect to reach that for a while, so it should be fine for now. I set it like this:
export const generateStaticParams = async () => {
const response = await getArticles(client, {
locale: "ja-JP",
"fields.publishTo": "ja",
limit: 1000,
});
return response?.items?.map((item) => ({
slug: item?.sys?.id,
}));
};