next.js: Inconsistent loading order of CSS Modules + global CSS

Bug report

Describe the bug

I have a global CSS imported in /pages/_app.js and name.module.css imported in a page. In production mode the CSS Module is loaded after the global CSS. In development mode, the CSS Module is loaded before the global CSS.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

Repository with code needed to reproduce the issue

  1. Add a global style within pages/_app.js
  2. Add and import a CSS Module
  3. Add CSS classes to both files with the same specificity, so the order of including files resolves the conflict.
  4. Run “dev” script.
  5. See order of CSS in a browser.
  6. Run “build” and “start” scripts.
  7. See different order of CSS in a browser.

Expected behavior

In development mode the global CSS should be loaded first and CSS Modules later on, the same as it works in production.

System information

  • OS: macOS 10.15.2
  • Browser: Chrome 79.0.3945.130
  • Version of Next.js: 9.2.0

Additional context

The page looks different in dev and production modes because of CSS overriding.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 39
  • Comments: 27 (13 by maintainers)

Most upvoted comments

If anyone on this thread is desperate for this fix, it is available in the 9.3.6-canary.0 next.js release (thanks @Timer for turning that around so quickly!)

We have the same issue using the version 9.3.0 with SASS globals in _app and the CSS modules for single components

9.4.4 did NOT fix the problem for me. However, @canary (currently 9.4.5-canary.7) DOES fix it in my case. Hope that helps.

Shouldn’t any CSS imported from CSS modules override global styles imported in the _app.js file?

If I land on a page with a component that imports a CSS module, the global styles are added after the module’s styles and overwrites its rules. But if I start the app and then go to the page with the component, the component’s CSS gets added after the global styles and therefire overrides them.

I would have thought the second version is the ideal behaviour. But it definitely shouldn’t be inconsistent no?

This problem is still there, is there any plan on fixing it?

I’m currently facing the same issue and it’s kinda frustrating. It’s stopping me from using next for now. 😕

What’s the progress with this? I can’t use the latest version on next because of this. I load two stylesheets in my project, bootstrap first then my styles. When I deploy to production the load order is reversed and all my styles are overwritten by bootstrap’s…

This is still not fixed yet, I still experienced with this issue in production mode in v9.4.4, I use custom CSS loader and use antd-design

@sparkertime this is awesome, thanks!

I faced this problem too.

In my _app.tsx, I imported single css file, which @imported other global styles. And this led to a problem. The solution was to import ALL global css files inside the _app.tsx.

Before:

// _app.tsx
import "../styles/index.css"

// styles/index.css
@import "./reset.css";
@import "./grid.css";

After:

// _app.tsx
import "../styles/reset.css";
import "../styles/grid.css";

Anyone have any ideas on how to resolve this? It’s making it difficult to use Next as intended.

Any update on this issue? It’s blocking my styles development using sass 😕

I’ve added a repository with minimum code that reproduces the issue https://github.com/nikkiselev/nextjs-css