Capítulo 442 de 456

Routing Information

Core Idea

Schema for the routing object passed to onBuildComplete, describing the ordered phases Next.js uses to match a request to a route, with pre-processed patterns ready for platform deployment.

Key Concepts

  • routing.beforeMiddleware: routes applied before middleware execution, including generated header/redirect behavior.
  • routing.beforeFiles: rewrite routes checked before filesystem route matching.
  • routing.afterFiles: rewrite routes checked after filesystem route matching.
  • routing.dynamicRoutes: dynamic matchers from segments like [slug] and catch-alls.
  • routing.onMatch: routes applied after a successful match (e.g. immutable cache headers for hashed static assets).
  • routing.fallback: final rewrite routes checked when earlier phases found no match.
  • Common route fields: source?, sourceRegex, destination?, headers?, has?, missing?, status?, priority?.

Key Takeaways

  1. As seis fases (beforeMiddlewarebeforeFiles → filesystem match → afterFilesdynamicRoutesonMatch/fallback) definem a ordem exata de matching que um adapter precisa reproduzir.
  2. onMatch é o lugar certo pra aplicar headers de cache imutável em assets com hash.
  3. Cada rota tem sourceRegex pré-compilado, evitando reimplementar a compilação de padrões no adapter.

Connects To

  • Routing with @next/routing (ch438): resolveRoutes() consome exatamente este objeto routing.
  • API Reference (ch436): onde context.routing é formalmente definido como parâmetro de onBuildComplete.