CSS Modules styles defined in wrong order in development mode
See original GitHub issueBug report
Describe the bug
In development mode, when a page imports styles from a .module.css
file ( import styles from "./home.module.css"
) and then imports another component after that ( import { Container } from "../components/Container"
), and passes the imported styles to the component like <Container className={styles.blue}>
, these styles get overriden by the imported component. This component has it’s own styles and passes the className
prop to a DOM element:
import styles from "./Container.module.css";
function classes(...classNames) {
const joined = classNames.join(' ');
return joined;
}
export const Container = ({
children,
className= "",
}) => {
return (
<div className={classes(styles.container, className)}>
{children}
</div>
);
};
The Container
has the following style:
.container {
background-color: yellow;
}
But the page provides the following style:
.blue {
background-color: blue;
}
In development, the styles are injected into the head as follows:
<style>
.index_blue__3Iu-s {
background-color: blue;
}
</style>
<style>
.Container_container__1UmhU {
background-color: yellow;
}
</style>
In production, these styles are combined into one stylesheet:
.Container_container__1UmhU {
background-color: #ff0
}
.index_blue__3Iu-s {
background-color: #00f
}
As you can see, there is a difference in order. This causes the inconsistency across environments with CSS ( yellow in development, blue in production ).
I believe that the production result is the correct one, as you should be able to override the component styles.
I’d be happy to try to contribute to a fix. However, I am a beginner developer so I might need some guidance.
To Reproduce
Clone my reproduction repository and run npm run dev
to see the styles applied incorrectly, and run npm run build; npm run start
to see that the styles work as intended.
Expected behavior
I would expect that in both development and production mode the classes get defined in the same order so that the styles are applied correctly.
System information
- OS: Windows 10
- Browser: Chrome 81.0.4044.138
- Version of Next.js: 9.4.1
- Version of Node.js: 12.16.1
More information
The style tags are injected by the code in this file. It uses style-loader
in development and the MiniCssExtractPlugin
in production. What is the reason as to why it isn’t used in development mode?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:14
- Comments:17 (8 by maintainers)
Top GitHub Comments
Going to keep this open because this is something that Next.js could improve. I’m willing to help work on it.
You might be right. Although I do still think the loading order should be consistent across environments