@aws-amplifygen3/adapter-nextjs
v1.0.2
Published
Amplify hosting adapter and build command for Next.js (native deployment adapters API, with an OpenNext compatibility path for older Next.js versions)
Maintainers
Readme
@aws-amplifygen3/adapter-nextjs
Next.js adapter and build command for the Amplify hosting platform. It emits a deployable .amplify bundle (deployment manifest, compute server, and static assets) through one of two paths, selected by the app's Next.js version:
- Next.js 16.2+ — native adapter. Next.js calls the adapter through its deployment adapters API at the end of
next build. - Next.js 14.2 – 16.1 — OpenNext compatibility path. The build command runs
next buildin standalone mode, then transforms the output with OpenNext.
Usage
As a build command (any supported version)
From your Next.js app directory, with the package installed as a dependency:
npx @aws-amplifygen3/adapter-nextjs buildThe command detects the Next.js version and takes the matching path. On 16.2+ it runs next build with the native adapter wired in through NEXT_ADAPTER_PATH.
As a Next.js adapter (Next.js 16.2+)
// next.config.js
module.exports = {
adapterPath: require.resolve("@aws-amplifygen3/adapter-nextjs"),
};Then run next build as usual.
Either way, output lands in .amplify/:
deployment-manifest.json— regex route rules, compute config, static assets, image settingsstatic/— client-side assets (_next/static/,favicon.ico, etc.)compute/default/— server bundle (see Output Structure below)
The @aws-amplifygen3/adapter-nextjs/build entry point exposes the same pipeline programmatically.
Package layout
src/— the native adapter (src/adapter.ts, the package's main entry)src/opennext/— the build command (cli.ts,build.ts) and the OpenNext compatibility pathdist/bundle/adapter.mjs— bundled native adapter, with its runtime templates indist/bundle/runtime/dist/bundle/opennext/cli.mjs— bundled build command (theadapter-nextjsbin), with its runtime templates indist/bundle/opennext/runtime/dist/bundle/node_modules/— dependencies vendored for both bundles
Package contents
The published package is compiled JavaScript with type declarations. The
.ts files under dist/bundle/runtime/ and dist/bundle/opennext/runtime/
are build-time templates, not sources to compile on install: the adapter
bundles them into the compute output of your app with esbuild during
next build. dist/bundle/node_modules/ vendors the adapter's own runtime
dependencies, except esbuild: its native binary is platform-specific, so it
is a declared dependency that your npm install provides for your platform.
Prerequisites
- Node.js >= 22
- npm (installed via workspace root)
Setup
From the workspace root:
npm installBuild
Compile TypeScript to dist/ and produce the bundles in dist/bundle/:
npm run buildTest
Unit + Property Tests (fast)
npm testRuns the unit and property tests for both paths: manifest generation, compute bundling, cache handlers, middleware, route generation, size validation, and error messages.
Integration Tests (requires network)
npm run test:integrationIncludes everything from npm test plus:
- OpenNext-path integration tests that dynamically scaffold a Next.js app (both 15.x and 16.x), build it, and verify the
.amplifyoutput structure, including runs from an isolated copy ofdist/bundle/ - Manifest snapshot tests
These tests run npm install and npx next build in a temp directory, so they require network access and take several minutes.
E2E Tests (hosting emulator)
npm run test:e2eBuilds fixture apps (test/fixtures/ for the native adapter, test/opennext/fixtures/ for the OpenNext path), starts the hosting emulator, and verifies HTTP responses for API routes, SSR pages, and server actions.
Update Snapshots
OpenNext-path snapshots are local-only (gitignored). When running integration tests for the first time or after manifest changes:
TEST_INTEGRATION=true npx vitest run --updateRun a Single Test File
npx vitest run test/manifest-generator.test.ts
npx vitest run test/opennext/createAmplifyBundle.test.ts
npx vitest run test/opennext/properties/size-validation.prop.test.tsDeploying
The .amplify directory produced by the build is the deployment artifact. Deploy it with the Amplify hosting tooling for your project.
Running the hosting emulator
The deployment-manifest package ships a local emulator that serves a built .amplify directory:
node node_modules/@aws-amplifygen3/deployment-manifest/dist/cjs/emulator/hosting-emulator.js --output-dir .amplifyThen verify routes:
curl http://localhost:8085/ # SSR home page
curl http://localhost:8085/api/hello # API route (JSON)
curl http://localhost:8085/blog/test # Dynamic route
curl http://localhost:8085/about # Static pageOutput Structure (OpenNext path)
.amplify/
├── deployment-manifest.json
├── static/
│ ├── _next/static/...
│ ├── favicon.ico
│ └── robots.txt
└── compute/
└── default/
├── index.js (runtime proxy entrypoint)
├── server.js (Next.js standalone server)
├── middleware.mjs (Next.js middleware bundle)
├── open-next.config.mjs (OpenNext config)
├── cache.cjs (cache handler)
├── composable-cache.cjs (composable cache handler)
├── patchedAsyncStorage.cjs
├── assets/ (server-side assets)
├── node_modules/ (runtime dependencies)
└── wasm/ (WebAssembly modules)For a monorepo (npm-workspaces) app, OpenNext nests the standalone server and
the server runtime entrypoint under the app's package path — index.js and
server.js live at compute/default/<packagePath>/ (e.g.
compute/default/apps/web/) instead of the compute root, while node_modules/
stays at the compute root. The deployment manifest's compute.default.entrypoint
points at the nested index.js accordingly.
Deployment Manifest
The adapter generates deployment-manifest.json conforming to the Amplify deployment specification. Key sections:
routes— ordered regex route rules: static assets (immutable cache), image optimization, per-page compute routes with trailing-slash redirects, error pages (404/500), catch-all fallbackcompute—NodeJs-24.xruntime with anindex.jsentrypoint. On the OpenNext path, a monorepo (npm-workspaces) app nests the standalone server under the app package path, so the entrypoint is declared as<packagePath>/index.js(e.g.apps/web/index.js) to match the actual bundle layoutstaticAssets— path to static directoryimageSettings— sizes, domains, remotePatterns, formats, minimumCacheTTL, dangerouslyAllowSVG (read from Next.js build output)
Route patterns use regex syntax (e.g. ^/api/hello$, ^/blog/([^/]+)$) with support for dynamic segments ([id]), catch-all segments ([...slug]), and optional catch-all segments ([[...path]]). Every pattern is checked against the platform's fancy-regex engine through @aws-amplifygen3/deployment-manifest/regex.
Build-Time Validations
- Compute bundle size ≤ 250 MB
- Individual static asset ≤ 100 MB
- Deployment manifest schema validation via
validateHostingManifest - Cache-control directive validation
- All errors follow
[Next.js] {step} failed: {details}. {remediation}format
Troubleshooting
| Issue | Fix |
| ------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| runtime proxy not found | Run npm run build in this package |
| standalone server.js not found | Add output: 'standalone' to next.config.js |
| Compute bundle size validation failed | Remove unused dependencies or check esbuild externals |
| Static asset size validation failed | Compress or remove the oversized file |
| require is not defined | Ensure the proxy uses createRequire (already fixed in current version) |
| Snapshot test fails after manifest change | Run npx vitest run --update and review the diff |
| 500 on routes importing pg / @aws-sdk/dsql-signer (Next 16.1.x) | Build with next build --webpack — see Turbopack and native server dependencies |
Turbopack and Native Server Dependencies
On Next.js 16.1.x, next build with Turbopack (the default) externalizes server
dependencies such as pg and @aws-sdk/dsql-signer under content-hashed names
(e.g. pg-587764f78a6c7a9c). Those hashed specifiers do not resolve inside the
deployed Lambda, so any route importing them returns a 500 at runtime.
Building with the webpack bundler instead resolves these correctly:
next build --webpackserverExternalPackages and declaring the packages as direct dependencies do
not work around this — use the webpack build until the upstream Turbopack
behavior changes.
Supported Next.js Versions
- Next.js 16.2+ — native adapter (build command or
adapterPath) - Next.js 16.0 – 16.1 — OpenNext compatibility path (experimental — some features may not work as expected)
- Next.js 14.2 – 15.x — OpenNext compatibility path (14.2 is the minimum)
