next.js: When building next js app, class names are minified which breaks libraries which depend on class names. (typegoose, webpack)

Link to the code that reproduces this issue

https://github.com/tar-aldev/broken-class-names

To Reproduce

First serve it in dev mode to see how it should work

  1. copy .env.example and set credentials (or leave them as is)
  2. run mongodb via docker docker-compose up
  3. run next.js app via npm run nx serve next-broken
  4. IN browser see how it shows proper model name BrokenModel

Now build app and run it in prod mode

  1. build the app with npm run nx build next-broken
  2. serve it with npm run nx run next-broken:serve:production
  3. open browser or check the terminal of the IDE
  4. model name becomes p while it should still be BrokenModel

Current vs. Expected behavior

I’d expect model names (based on class name) to not be minified as it breaks typegoose. It worked fine in next.js 13 (“next”: “13.4.19”)

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.0.0: Fri Sep 15 14:41:43 PDT 2023; root:xnu-10002.1.13~1/RELEASE_ARM64_T6000
Binaries:
  Node: 18.18.2
  npm: 9.8.1
  Yarn: N/A
  pnpm: 8.9.2
Relevant Packages:
  next: 14.0.5-canary.10
  eslint-config-next: 13.4.4
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.2.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Additional context

It breaks since next 14 If I disable minification in next.conif.js

webpack: (config) => {
    config.optimization.minimize = false;
    return config;
  },

Then it works, but the build is not optimized which is not acceptable for prod.

As a workaround I could use TerserPlugin and set keep_classnames: true and then it works fine. (see with-terser` branch in github repo. But it would be nice to get an official guideline.

About this issue

  • Original URL
  • State: open
  • Created 7 months ago
  • Reactions: 9
  • Comments: 22 (3 by maintainers)

Most upvoted comments

Yes, this is because I forgot to change repository’s visibility, so bot wasn’t able to confirm reproduction URL and issue got closed. Also, I think this is where the issue occurred first: https://github.com/vercel/next.js/commit/30da48fcd28b1fc07dcfa06d89b28bec95897d10 They enabled minification for server side code, which is now handled by SWC afaik and it seem to me that SWC breaks class names for decorated classes.

So, the current solution would be just to disable minification for server code, like so (in next.config.js):

/** @type {import('next').NextConfig} */
module.exports = {
  experimental: {
    serverMinification: false,
  }
}

Can confirm this also causes issues with TypeORM. We kept getting the following error message: CircularRelationsError: Circular relations detected: Dependency Cycle Found: s -> s. To resolve this issue you need to set nullable: true somewhere in this dependency structure.

Setting serverMinification: false resolves the issue.

I can confirm this is also an issue on v14 with TypeOrm. serverMinification: false works but instead, i went to my entity decorators and manually set the table names. Not minifying feels icky.

Could we have a bit more granularity here? Bailing out of all server minification is not exactly an ideal solution if you want to have a compact docker container

Similar scenario using Nextjs and Mikro-orm. Running in development mode all is good. When I export and run the standalone build, database table names are minified, hence breaking the functionality. A findAll() on a Log entity returns this and Postgres complains:
'select "g0".* from "g" as "g0" - relation "g" does not exist'. I tried first with swcMinify: false in my next.config.js and it did not change anything. Using @octet-stream’s suggestion and setting serverMinification: false in the experimental block works and solves the issue.

  • Folder size .next/standalone with serverMinification: true: 57Mb
  • Folder size .next/standalone with serverMinification: false: 58Mb

So, not a big price to pay to have it working… I however very much dislike using anything called experimental in production.