The generateStaticParams
function can be used in combination with dynamic route segments to statically generate routes at build time instead of on-demand at request time.
export async function generateStaticParams() {
const posts = await fetch('https://.../posts').then((res) => res.json());
return posts.map((post) => ({
slug: post.slug,
}));
}
The primary benefit of the generateStaticParams
function is its smart retrieval of data. If content is fetched within the generateStaticParams
function using a fetch
request, the requests are automatically deduplicated. This means a fetch
request with the same arguments across multiple generateStaticParams
, Layouts, and Pages will only be made once, which decreases build times.
Use the migration guide if you are migrating from the pages
directory.
See generateStaticParams
server function documentation for more information and advanced use cases.