`ValidationError` becomes `o` after Next.js build
Describe the Bug
When using Payload CMS with Next.js, the error name changes after build:
- In
next dev:"name": "ValidationError" - In
next start:"name": "o"
This happens because Next.js minifies class names in production.
Solution
Disable server-side minification in next.config.js:
/** @type {import('next').NextConfig} */
const nextConfig = {
distDir: 'dist',
experimental: {
serverMinification: false,
},
}
https://github.com/vercel/next.js/issues/59594
Link to the code that reproduces this issue
https://github.com/payloadcms/payload/blob/main/.github/reproduction-guide.md
Reproduction Steps
next build
Which area(s) are affected? (Select all that apply)
area: core
Environment Info
"@payloadcms/db-postgres": "^3.50.0",
"@payloadcms/next": "^3.50.0",
"@payloadcms/ui": "^3.50.0",
"next": "15.1.5",
"payload": "^3.50.0",
my workaround for this:
import {
AuthenticationError,
LockedAuth,
UnverifiedEmail,
ValidationError,
} from "payload";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ErrorClassType = new (...args: any[]) => any;
function preserveErrorName(ErrorClass: ErrorClassType, originalName: string) {
Object.defineProperty(ErrorClass.prototype, "name", {
get() {
return originalName;
},
set() {},
configurable: true,
enumerable: false,
});
}
export function preserveErrorNames() {
preserveErrorName(AuthenticationError, "AuthenticationError");
preserveErrorName(LockedAuth, "LockedAuth");
preserveErrorName(UnverifiedEmail, "UnverifiedEmail");
preserveErrorName(ValidationError, "ValidationError");
}
Hi @nathanbowang , this looks directly related to the issue I created, Production build of Payload not respecting loggingLevels configuration #13532 .
I tried your simple fix of disabling serverMinification, and it resolved my issue of validation errors incorrectly being logged as ERROR on production build.
@mulfyx , can you explain your fix a little more, where is preserveErrorNames referenced?
How does it work?
@rjohnsonbade-corrivium just import in payload.config.ts and call preserveErrorNames(); before export default defineConfig({ ... });
Environment
- Payload: 3.86.0
- Next.js: 16.2.6 (Turbopack)
- Node: 22.x / Alpine
Bug Analysis: instanceof ValidationError fails under Turbopack due to Chunk Boundaries
While the recent patch to use instanceof ValidationError addresses the standard
SWC class-name minification issue, it introduces a silent failure when building
with Next.js 16 + Turbopack (next build with native Turbo compilation enabled
as of Payload 3.82+).
The Root Cause
Under Turbopack's production chunk-splitting model, the error handling middleware
and the module throwing the error can be bundled across distinct chunk boundaries.
This results in isolated module instances. When an error is thrown from a separate
chunk, instanceof ValidationError evaluates to false because the prototypes
originate from two different evaluated instances of the class.
The Solution
To make the validation robust against both class name minification and Turbopack chunk boundaries, we should shift from prototype validation to structural contract validation.
By checking Payload's explicit operational footprint, we can safely identify the error across chunk lines:
// Replace prototype checks with structural contract checks
if (incoming.data != null && incoming.isOperational === true) {
// Safe to extract validation payload without prototype matching
}
Debug Proof
incoming instanceof ValidationError: false ← fails across chunk boundary
incoming instanceof APIError: false
incoming.data: {"collection":"...","errors":[...]} ← data IS present
incoming.isOperational: true
incoming.name: e ← minified class name
Confirming on Payload 3.89 / Next 16.3 (Turbopack): serverMinification: false has no effect under Turbopack, and turbopackMinify: false un-minifies client bundles too (3.8 MB → 9.2 MB). Also agree instanceof isn't reliable. We saw it fail for errors thrown via /api/graphql, and separately the GraphQL handler logs the GraphQLError wrapper, so GraphQL 403s log at error even in dev. Our workaround (Claude assisted) is a pino logMethod hook that matches on originalError ?? err, isOperational and status.
Same on Payload 3.88.0 / Next 16 (Turbopack build). An APIError thrown from a collection beforeValidate hook: error instanceof APIError is true in a config afterError hook, but formatErrors drops data. payload/dist/errors/APIError.js is emitted in two server chunks under the same module id. Worked around with an afterError hook that restores data for our own errors.