Q4: How does getStaticPaths work alongside getStaticProps for dynamic routes?

When using dynamic routes (e.g., pages/posts/[id].js) with getStaticProps, getStaticPaths defines the list of path instances that must be pre-rendered to static HTML during build time.

Defining Static Path Instances
// pages/posts/[id].js
export async function getStaticPaths() {
  return {
    paths: [
      { params: { id: '1' } },
      { params: { id: '2' } }
    ],
    fallback: false,
  };
}

Returns an array of path parameter objects that Next.js will pre-render statically during build.

Key Takeaways
  • Defines which dynamic parameter paths to pre-render at build time.

  • Must be paired with getStaticProps on dynamic routes.

Knowledge Check

Which function MUST be used alongside getStaticProps on dynamic routes like pages/blog/[slug].js?