payload icon indicating copy to clipboard operation
payload copied to clipboard

Migration JSON schema files cause Vercel serverless function size limit failures

Open jellologic opened this issue 10 months ago • 4 comments

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

  1. Documentation: Add a note about this in the migrations docs for Vercel users

  2. Feature: Consider a config option like excludeMigrationSnapshotsFromBundle: true that would:

    • Move JSON snapshots to a separate directory not traced by NFT
    • Or dynamically load them only when running payload migrate:create
  3. 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!

jellologic avatar Feb 03 '26 21:02 jellologic

Have you considered moving your migrations outside of the src/ directory?

jackmaders avatar Feb 06 '26 16:02 jackmaders

@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.

jellologic avatar Feb 09 '26 22:02 jellologic

Could you check if this release fixes it? 3.78.0-internal.5219978

AlessioGr avatar Feb 25 '26 19:02 AlessioGr

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 from 3.75.0 to 3.78.0-internal.5219978
  • Also configured prodMigrations in the DB adapter to bypass readMigrationFiles() 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/*.json before next 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:

  1. Move readMigrationFiles behind a conditional dynamic import so NFT can't trace it when prodMigrations is configured
  2. Add outputFileTracingExcludes for *.json files in the migrations directory in withPayload()
  3. Move JSON schema snapshots to a subdirectory (e.g., migrations/.snapshots/) that isn't read by fs.readdirSync in readMigrationFiles
  4. Use eval() to hide fs.readdirSync from NFT (similar to how dynamicImport uses eval() to hide import() 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

jellologic avatar Mar 09 '26 18:03 jellologic