question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CSS Modules styles defined in wrong order in development mode

See original GitHub issue

Bug 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:open
  • Created 3 years ago
  • Reactions:14
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

8reactions
jensmeindertsmacommented, Jun 2, 2020

Going to keep this open because this is something that Next.js could improve. I’m willing to help work on it.

2reactions
jensmeindertsmacommented, Jun 2, 2020

You might be right. Although I do still think the loading order should be consistent across environments

Read more comments on GitHub >

github_iconTop Results From Across the Web

reactjs - CSS module classes work on development, but are ...
Show activity on this post. All the styles on my project are defined inside css modules, and only recently I noticed that, on...
Read more >
Debugging CSS - Learn web development | MDN
In order to do so I'll be using an example file. Load this up in a new tab if you want to follow...
Read more >
Solving the React Error: Not Picking Up CSS Style | Pluralsight
Front End Web Development ... CSS files are the core components of a frontend developer project. ... Styling the wrong id or class....
Read more >
What are CSS Modules and why do we need them?
title class accessible via styles.title . Our build step would then process both these things into new, separate HTML and CSS files, with...
Read more >
Why We're Breaking Up with CSS-in-JS - DEV Community ‍ ‍
Note: CSS Modules also allow you to colocate styles with components ... have to be defined as both a CSS variable and a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found