Skip to content

fix(@vercel/next): merge next-minimal-server.js.nft.json into server trace for prebuilt deployments - #15677

Closed
sleitor wants to merge 3 commits into
vercel:mainfrom
sleitor:fix-15654
Closed

sleitor wants to merge 3 commits into
vercel:mainfrom
sleitor:fix-15654

Conversation

@sleitor

@sleitor sleitor commented Mar 22, 2026

Copy link
Copy Markdown
Contributor

What

When using prebuilt deployments (vercel build + vercel deploy --prebuilt) with Turbopack and the Pages Router, vendored context files required by require-hook.js at runtime were missing from the serverless function bundle. This caused MODULE_NOT_FOUND errors for files like head-manager-context.js on pages using next/head or next/error.

Root cause

next-server.js.nft.json (the trace used in the non-bundled path) does not include vendored context files that require-hook.js rewrites at runtime. next-minimal-server.js.nft.json (produced by Turbopack builds) does include all 10 vendored context files, but it was only consumed when the internal VERCEL_NEXT_BUNDLED_SERVER=1 env var was set — which is only true on Vercel infrastructure, not during vercel build.

Fix

After reading next-server.js.nft.json, also attempt to read next-minimal-server.js.nft.json. If it exists:

  • Merge its files into the existing trace (adding any files not already present)
  • If no next-server.js.nft.json exists, use it as a fallback

This is a non-breaking additive change: it only activates when !useBundledServer and the Next.js version supports bundled server (>= 13.5.4), and silently no-ops if next-minimal-server.js.nft.json is unavailable.

Testing

The fix can be verified by:

  1. Building a Pages Router app with Turbopack (vercel build --prod)
  2. Checking that .vercel/output/functions/<func>.func/ now includes all 10 vendored context files
  3. Comparing with VERCEL_NEXT_BUNDLED_SERVER=1 vercel build --prod — both should produce the same vendored file list

Fixes #15654

@sleitor
sleitor requested review from a team, huozhi, ijjk, timneutkens and ztanner as code owners March 22, 2026 02:35
@changeset-bot

changeset-bot Bot commented Mar 22, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 564f4c0

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

This PR includes changesets to release 2 packages
Name Type
@vercel/next Patch
vercel 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

@vercel

vercel Bot commented Mar 22, 2026 •

Copy link
Copy Markdown
Contributor

Warning

High Risk — Removes env var check, enabling bundled server path for all deployments — could break builds if bundled server path has issues.

  • server-build.ts: removed VERCEL_NEXT_BUNDLED_SERVER === '1' env var requirement from useBundledServer condition
  • .changeset/fix-prebuilt-bundled-server-trace.md: added changeset describing fix for prebuilt deployments

Assessed at 564f4c0.

Comment thread packages/next/src/server-build.ts Outdated
// newer Next.js builds), merge its files into the trace so prebuilt
// deployments include all necessary vendored files.
if (
!useBundledServer &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think we can remove VERCEL_NEXT_BUNDLED_SERVER env check, this was a flag that we used to roll out the bundled server way back when it was new but has been fully rolled out for a long time now.

…trace for prebuilt deployments

When using prebuilt deployments (vercel build + vercel deploy --prebuilt) with
Turbopack and the Pages Router, vendored context files required by require-hook.js
at runtime were missing from the serverless function bundle. The next-server.js.nft.json
trace does not include vendored paths that require-hook.js rewrites at runtime.

This fix reads next-minimal-server.js.nft.json (when it exists) and merges its
files into the server trace, capturing all vendored context files (e.g.
head-manager-context.js) that newer Next.js builds produce via Turbopack.
The internal VERCEL_NEXT_BUNDLED_SERVER env var is no longer required for
prebuilt deployments to get the correct file list.

Fixes vercel#15654
@sleitor

sleitor commented Mar 22, 2026

Copy link
Copy Markdown
Contributor Author

Done! Removed the VERCEL_NEXT_BUNDLED_SERVER env check — the condition now simply checks the semver version. Thanks for the clarification, makes sense that this flag has been fully rolled out for a while.

Force-pushed the updated commit.

Comment thread packages/next/src/server-build.ts Resolved
@sleitor

sleitor commented Mar 23, 2026

Copy link
Copy Markdown
Contributor Author

Good catch! Removed the dead code block — since useBundledServer is now always true (every supported Next.js version is ≥ v13.5.4), the !useBundledServer && semver.gte(...) branch could never execute. The merge logic is no longer needed since the bundled server path already reads next-minimal-server.js.nft.json directly. Force-pushed the cleanup.

@ijjk ijjk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, going to land in #15688 as tests can't run against forks currently

@ijjk ijjk closed this in #15688 Mar 23, 2026
ijjk added a commit that referenced this pull request Mar 23, 2026
@sleitor
sleitor deleted the fix-15654 branch March 23, 2026 19:49

This branch was previously deployed

1 inactive deployment
Preview — 564f4c0f Deployed Mar 23, 2026 by vercel[bot]
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.

Prebuilt deployments with Turbopack miss vendored Pages Router context files at runtime

2 participants