Deviopath › Next.js

Static or Dynamic? Route Classifier

Hard · Rendering

Without Cache Components (the "previous model", still used when cacheComponents is off), next build decides for every route whether it can be prerendered or must be rendered on each request. Using a Request-time API (await cookies(), await headers(), await connection(), reading searchParams) or an uncached fetch makes the route dynamic. The dynamic route segment config can override this. This is a simplified model of those rules.

Write classifyRoutes(routes). Each route is { path, dynamic?, revalidate?, generateStaticParams?, uses?, fetches? }: dynamic is "auto" (default), "force-static", "force-dynamic" or "error"; revalidate is the segment config (false, 0 or seconds); uses lists Request-time APIs used; fetches lists { cache?, revalidate? } options of each fetch. A route is a dynamic route if its path contains [.

An uncached fetch is one with cache: "no-store" or revalidate: 0. Decide the mode:

- "force-dynamic": dynamic.

- "force-static": Request-time APIs return empty values and data is cached, so nothing makes it dynamic. Mode is ssg for a dynamic route (generated on first visit if there is no generateStaticParams), otherwise static.

- "error": if uses is not empty, it is a build error with reason uses[0]. Else if there is an uncached fetch, the reason is fetch no-store or fetch revalidate 0 for the first one. Otherwise decide like "force-static".

- "auto": dynamic if uses is not empty, any fetch is uncached, or the segment revalidate is 0. Otherwise a dynamic route is ssg when generateStaticParams is true and dynamic when it is not. A route without dynamic segments is static.

For static and ssg results, the route revalidates at the lowest positive number among the segment revalidate and the fetch revalidate values (if any). Return one string per route, in order:

- "<path>: static", "<path>: ssg" or "<path>: dynamic"

- with a revalidate time: "<path>: static (revalidate 60s)"

- errors: "<path>: error (cookies)"

Next: Optimistic Cart with useOptimistic