Skip to content

fix(aws): copy server/instrumentation.js into standalone dir on Next.js 16#1195

Open
JiahaoZhu11 wants to merge 1 commit into
opennextjs:mainfrom
JiahaoZhu11:fix/next16-standalone-instrumentation
Open

fix(aws): copy server/instrumentation.js into standalone dir on Next.js 16#1195
JiahaoZhu11 wants to merge 1 commit into
opennextjs:mainfrom
JiahaoZhu11:fix/next16-standalone-instrumentation

Conversation

@JiahaoZhu11

Copy link
Copy Markdown

Problem

On Next.js 16, open-next build fails during the server-function bundle:

Error: This error should only happen for static 404 and 500 page from page router. Report this if that's not the case.,
        File server/instrumentation.js does not exist
    at computeCopyFilesForPage (.../build/copyTracedFiles.js)
    at copyTracedFiles (...)
    at createServerBundle (...)

Root cause

Next 16's output: "standalone" no longer copies server/instrumentation.js into the standalone output dir (it remains only in .next/server/). In copyTracedFiles, the instrumentation block copies the instrumentation .nft.json trace into standaloneNextDir and then calls computeCopyFilesForPage("instrumentation"), which asserts that server/instrumentation.js exists in standaloneNextDir — so it throws. (Middleware is unaffected because its .js is emitted into the standalone dir.)

Fix

Before the assertion, copy server/instrumentation.js from the build dir into the standalone dir — mirroring the existing .nft.json copy right above it — guarded so it only runs when the file exists in the build dir and isn't already present in the standalone dir. Pre-16 behavior is unchanged, and the instrumentation file ships in the bundle.

Validation

Reproduced on a real pnpm monorepo app: Next.js 16.1.6, App Router, next-intl middleware, @opennextjs/aws@4.0.3, monorepo config (packageJsonPath / appPath / buildOutputPath) + output: "standalone" + outputFileTracingRoot.

  • Before: build fails at copyTracedFiles with the error above.
  • After (this change applied as a patch to 4.0.3): open-next build completes; the full bundle is produced (server-functions/default, assets, cache, dynamodb-provider, revalidation-function, image-optimization-function, warmer-function); the server-function bundle loads and exports handler; server/instrumentation.js is present in the server-function output.

A patch changeset is included.

🤖 Generated with Claude Code

Next.js 16's standalone output no longer includes server/instrumentation.js in
the standalone directory. copyTracedFiles copies the instrumentation .nft.json
trace and then asserts the .js exists in standaloneNextDir, throwing
"File server/instrumentation.js does not exist" during the server bundle.

Copy the instrumentation .js from the build dir into the standalone dir
(mirroring the existing .nft.json copy) so the assertion passes and the file
ships in the bundle.
@changeset-bot

changeset-bot Bot commented Jul 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 2629b67

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@opennextjs/aws Patch
app-pages-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant