experimental.adapterPath
Configure a custom adapter for Next.js to hook into the build process with modifyConfig and onBuildComplete callbacks.
Next.js provides an experimental API that allows you to create custom adapters to hook into the build process. This is useful for deployment platforms or custom build integrations that need to modify the Next.js configuration or process the build output.
Configuration
To use an adapter, specify the path to your adapter module in experimental.adapterPath
:
Creating an Adapter
An adapter is a module that exports an object implementing the NextAdapter
interface:
Basic Adapter Structure
Here's a minimal adapter example:
API Reference
modifyConfig(config, context)
Called for any CLI command that loads the next.config to allow modification of the configuration.
Parameters:
config
: The complete Next.js configuration objectcontext.phase
: The current build phase (see phases)
Returns: The modified configuration object (can be async)
onBuildComplete(context)
Called after the build process completes with detailed information about routes and outputs.
Parameters:
routes
: Object containing route manifests for headers, redirects, rewrites, and dynamic routesroutes.headers
: Array of header route objects withsource
,sourceRegex
,headers
,has
,missing
, and optionalpriority
fieldsroutes.redirects
: Array of redirect route objects withsource
,sourceRegex
,destination
,statusCode
,has
,missing
, and optionalpriority
fieldsroutes.rewrites
: Object withbeforeFiles
,afterFiles
, andfallback
arrays, each containing rewrite route objects withsource
,sourceRegex
,destination
,has
, andmissing
fieldsroutes.dynamicRoutes
: Array of dynamic route objects withsource
,sourceRegex
,destination
,has
, andmissing
fields
outputs
: Detailed information about all build outputs organized by typeprojectDir
: Absolute path to the Next.js project directoryrepoRoot
: Absolute path to the detected repository rootdistDir
: Absolute path to the build output directoryconfig
: The final Next.js configuration (withmodifyConfig
applied)nextVersion
: Version of Next.js being usedbuildId
: Unique identifier for the current build
Output Types
The outputs
object contains arrays of different output types:
Pages (outputs.pages
)
React pages from the pages/
directory:
API Routes (outputs.pagesApi
)
API routes from pages/api/
:
App Pages (outputs.appPages
)
React pages from the app/
directory with page.{js,ts,jsx,tsx}
:
App Routes (outputs.appRoutes
)
API and metadata routes from app/
with route.{js,ts,jsx,tsx}
:
Prerenders (outputs.prerenders
)
ISR-enabled routes and static prerenders:
Static Files (outputs.staticFiles
)
Static assets and auto-statically optimized pages:
Middleware (outputs.middleware
)
Middleware function (if present):
Routes Information
The routes
object in onBuildComplete
provides complete routing information with processed patterns ready for deployment:
Headers
Each header route includes:
source
: Original route pattern (e.g.,/about
)sourceRegex
: Compiled regex for matching requestsheaders
: Key-value pairs of headers to applyhas
: Optional conditions that must be metmissing
: Optional conditions that must not be metpriority
: Optional flag for internal routes
Redirects
Each redirect route includes:
source
: Original route patternsourceRegex
: Compiled regex for matchingdestination
: Target URL (can include captured groups)statusCode
: HTTP status code (301, 302, 307, 308)has
: Optional positive conditionsmissing
: Optional negative conditionspriority
: Optional flag for internal routes
Rewrites
Rewrites are categorized into three phases:
beforeFiles
: Checked before filesystem (including pages and public files)afterFiles
: Checked after pages/public files but before dynamic routesfallback
: Checked after all other routes
Each rewrite includes source
, sourceRegex
, destination
, has
, and missing
.
Dynamic Routes
Generated from dynamic route segments (e.g., [slug]
, [...path]
). Each includes:
source
: Route patternsourceRegex
: Compiled regex with named capture groupsdestination
: Internal destination with parameter substitutionhas
: Optional positive conditionsmissing
: Optional negative conditions
Use Cases
Common use cases for adapters include:
- Deployment Platform Integration: Automatically configure build outputs for specific hosting platforms
- Asset Processing: Transform or optimize build outputs
- Monitoring Integration: Collect build metrics and route information
- Custom Bundling: Package outputs in platform-specific formats
- Build Validation: Ensure outputs meet specific requirements
- Route Generation: Use processed route information to generate platform-specific routing configs