Capítulo 272 de 456

Creating an Adapter

Core Idea

An adapter is a module exporting an object that implements the NextAdapter interface (imported from next), with two lifecycle hooks: modifyConfig and onBuildComplete.

Key Concepts

  • NextAdapter: TypeScript interface with name: string, optional modifyConfig, optional onBuildComplete.
  • modifyConfig(config, ctx): called for any CLI command loading next.config.js; receives { phase, nextVersion, projectDir } and returns the (possibly modified) config, sync or async.
  • onBuildComplete(ctx): called after build completes; receives routing (routing phases), outputs (AdapterOutputs), projectDir, repoRoot, distDir, config, nextVersion, buildId.
  • AdapterOutputs: { pages, middleware?, appPages, pagesApi, appRoutes, prerenders, staticFiles } — arrays of typed build output entries.
  • Route: routing rule shape (source?, sourceRegex, destination?, headers?, has?, missing?, status?, priority?).

Code Examples

import type { NextAdapter } from 'next'
/** @type {import('next').NextAdapter} */
const adapter = {
  name: 'my-custom-adapter',

  async modifyConfig(config, { phase }) {
    if (phase === 'phase-production-build') {
      return { ...config /* Add your modifications */ }
    }
    return config
  },

  async onBuildComplete({ routing, outputs, buildId }) {
    console.log('Build completed with', outputs.pages.length, 'pages')
    console.log('Build ID:', buildId)
    console.log('Dynamic routes:', routing.dynamicRoutes.length)

    for (const page of outputs.pages) {
      console.log('Page:', page.pathname, 'at', page.filePath)
    }
    for (const apiRoute of outputs.pagesApi) {
      console.log('API Route:', apiRoute.pathname, 'at', apiRoute.filePath)
    }
  },
}

module.exports = adapter
  • O que demonstra: skeleton mínimo de adapter com os dois hooks e iteração sobre outputs tipados.

Reference Tables

HookWhenKey params
modifyConfigAny CLI command that loads configconfig, phase, nextVersion, projectDir
onBuildCompleteAfter build finishesrouting, outputs, projectDir, repoRoot, distDir, config, nextVersion, buildId

Key Takeaways

  1. An adapter is plain JS/TS exporting { name, modifyConfig?, onBuildComplete? } — no base class or framework boilerplate.
  2. modifyConfig runs on every config load (dev, build, etc.), gated by phase when the adapter should only act during production builds.
  3. onBuildComplete is the single hook exposing the full output graph (outputs.*) and routing table for platform-specific processing.

Connects To

  • ch273 api-reference-1: full parameter/field documentation for both hooks.
  • ch279 output-types: detailed shape of each outputs.* array entry.
  • ch280 routing-information: detailed shape of the routing object.