My codebase has an extensive Content Security Policy (CSP) setup. I.e. many HTTP headers. Vercel is therefore giving the error ‘Builder returned invalid routes: should NOT be longer than 4096 characters’ during Vercel builds.
Please consider this discussion as a feature request and opportunity to help others.
I would expect this error to happen on localhost in a next build since the Vercel-Next.js relation is strong.
My solution was to relax the CSP config. Instead of listing hardcoded domains, I’ve basically reverted to a ‘allow most’ setup. I think Vercel’s technical setup should NOT reduce security.
// Be advised, if we configure connectSrc in a strict setup, we need all
// Google TLDs in here, making the header list too long for usage on Vercel.
// @see https://community.vercel.com/t/builder-returned-invalid-routes-should-not-be-longer-than-4096-char/9624
connectSrc: ["'self'", 'https:', ...(process.env.NODE_ENV === 'development' ? ['http:'] : [])],