Migration JSON schema files cause Vercel serverless function size limit failures
Summary
Migration JSON schema snapshot files can cause Vercel serverless functions to exceed the 250MB unzipped size limit. These files are only needed when generating new migrations, not at runtime, but they get bundled into every serverless function.
Environment
- Payload CMS: 3.69.0
- Next.js: 16.0.10
- Deployment: Vercel
- Database: PostgreSQL (Neon) via
@payloadcms/db-vercel-postgres
The Problem
Each migration creates a .json schema snapshot file alongside the .ts migration file. Over time, these accumulate:
src/migrations/
├── 20250123_181643_first_migration.json (147KB)
├── 20250123_181643_first_migration.ts (47KB)
├── 20250124_180431.json (149KB)
├── 20250124_180431.ts (2KB)
... (80+ migrations)
Total sizes in our project:
- JSON schema files: 60MB
- TS migration files: 1.2MB
The JSON files get traced by Next.js's @vercel/nft (node-file-trace) and bundled into every serverless function, pushing them over Vercel's 250MB limit.
Why outputFileTracingExcludes Doesn't Work
We tried various path formats in next.config.ts:
outputFileTracingExcludes: {
"/*": ["./src/migrations/**"],
// Also tried: "src/migrations/**", "**/migrations/**", etc.
}
None worked because the files are traced via file system reads during the Payload initialization, not just static imports. outputFileTracingExcludes only works for files that are traced but not actively read.
The Workaround
Delete the JSON files after migrations run but before the Next.js build (only on Vercel):
{
"scripts": {
"prebuild": "... && ([ -n \"$VERCEL\" ] && rm -f src/migrations/*.json || true)"
}
}
Results:
- Serverless function size: 248MB → 67MB (-73%)
- Build time: 25-40 min (failing) → 6 min (passing)
Suggestions
-
Documentation: Add a note about this in the migrations docs for Vercel users
-
Feature: Consider a config option like
excludeMigrationSnapshotsFromBundle: truethat would:- Move JSON snapshots to a separate directory not traced by NFT
- Or dynamically load them only when running
payload migrate:create
-
Default behavior: Consider not including JSON snapshots in the traced output since they're only needed for migration generation, not runtime
Related
- Vercel serverless function size limit: https://vercel.link/serverless-function-size
- Next.js outputFileTracingExcludes: https://nextjs.org/docs/app/api-reference/config/next-config-js/output
Happy to provide more details or help with a PR if there's interest in a built-in solution!
Have you considered moving your migrations outside of the src/ directory?
@jackmaders Thanks for the suggestion! We tried moving migrations outside of src/ (to next/migrations/) with migrationDir set in the db adapter config.
Unfortunately it didn't help — the serverless function still exceeded 250MB. Payload's findMigrationDir reads the directory at runtime regardless of its location, so @vercel/nft traces the JSON schema snapshot files either way.
Our workaround of deleting the .json files after migrations run (in prebuild) remains the only reliable fix for us:
"prebuild": "... && ([ -n \"$VERCEL\" ] && rm -f src/migrations/*.json || true)"
This drops the bundle from ~310MB to ~67MB. The JSON snapshots are only needed by payload migrate:create for generating diffs, not at runtime, so removing them before the Next.js build is safe.
Could you check if this release fixes it? 3.78.0-internal.5219978
Test Results for 3.78.0-internal.5219978
Tested on our production codebase (94 migration files, 86MB of JSON schema snapshots).
Setup
- Upgraded all
@payloadcms/*packages from3.75.0to3.78.0-internal.5219978 - Also configured
prodMigrationsin the DB adapter to bypassreadMigrationFiles()at runtime:import { migrations } from "./migrations"; db: vercelPostgresAdapter({ // ... prodMigrations: migrations, }),
Results
❌ The canary version alone does NOT fix the issue.
- Without
rm -f src/migrations/*.json→ Build fails:A Serverless Function has exceeded the unzipped maximum size of 250 MB - With
rm -f src/migrations/*.jsonbeforenext build→ Build succeeds (7m, same as production)
Root Cause Analysis
The prodMigrations config works at runtime — connect.js checks if (process.env.NODE_ENV === 'production' && this.prodMigrations) and passes them directly to migrate(), bypassing readMigrationFiles().
However, @vercel/nft performs static analysis at build time. It traces readMigrationFiles.js which contains fs.readdirSync(dir) on the migrations directory. Even though this code path isn't executed at runtime with prodMigrations, NFT doesn't know that — it sees the directory read and includes ALL files in the directory (including the .json schema snapshots) in the serverless bundle.
What Would Fix It
To actually prevent NFT from tracing the migration JSONs, Payload would need to either:
- Move
readMigrationFilesbehind a conditional dynamic import so NFT can't trace it whenprodMigrationsis configured - Add
outputFileTracingExcludesfor*.jsonfiles in the migrations directory inwithPayload() - Move JSON schema snapshots to a subdirectory (e.g.,
migrations/.snapshots/) that isn't read byfs.readdirSyncinreadMigrationFiles - Use
eval()to hidefs.readdirSyncfrom NFT (similar to howdynamicImportuseseval()to hideimport()from the bundler)
Our Workaround (still required)
# In prebuild.sh, after migrations run but before next build:
if [ -n "$VERCEL" ]; then
rm -f src/migrations/*.json
fi
This drops the serverless bundle from ~310MB to ~67MB. The JSON snapshots are only needed by payload migrate:create for generating diffs, not at runtime.
Test branch: test/payload-canary-migration-fix