Replies: 6 comments
|
This solution generated by Claude sonnet worked for me using NextJs 15.3.9 Why it's safe:
|
|
I'd be a bit careful with the 5-character hash here. The Also, for the setup in the question, this only affects the Webpack build. The So this looks like a Webpack-specific workaround, with the collision and Turbopack differences above. |
|
_Yes, it is possible to customize the generated classNames for CSS/SCSS Modules in Next.js! // 2. Webpack Configuration (Production) }, module.exports = nextConfig; _Key Points to Note |
|
A possible approach is to customize the existing
Next.js exposes the underlying Webpack config through One important detail for your setup is that development uses Turbopack. Turbopack handles CSS Modules natively, so a So a practical setup would be:
If you also want the custom naming to apply during development, Next.js supports running the dev server with Webpack instead:
There is also a related Next.js discussion about changing CSS Module output (#76887), where the existing CSS loader rule is modified rather than adding a new CSS rule: |
|
Yes you can customize the generated class names, but with the caveat that Turbopack in development ignores the webpack hook while the production Webpack build can be overridden. In next.config.js you can walk the css-loader rules and replace getLocalIdent with a short deterministic hash (for example a 6-character base64url SHA-1 of the relative file path plus the export name, prefixed with an underscore). This only affects the production build; if you also want short names in development run |
|
Yes, you can customize CSS Module class names for both Turbopack (dev) and Webpack (production) via next.config.js. next.config.ts / next.config.js // 2. Webpack (Production) module.exports = nextConfig; Key details:
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Summary
Hello everyone,
I'm working on a Next.js project using CSS Modules and SCSS Modules (*.module.css / *.module.scss).
By default, Next.js generates classNames like:
In my case, these classNames become very long, especially in production, and I'd like to:
Either shorten them
Or generate short hash-based classNames (e.g.
s0mTRRSk,lSI7qnOB)Ideally keep readable names in dev and short hashes in production (if possible)
I've tried looking into Webpack config and css-loader options, but Next.js abstracts most of this away.
My questions:
Is it possible to customize the generated classNames in Next.js when using CSS/SCSS Modules?
Can this be controlled differently for development vs production?
Is there an officially supported way, or does it require overriding Webpack config?
Any guidance or best practice would be appreciated.
Thanks 🙏
Additional information
All reactions