Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent loading order of CSS Modules + global CSS #10148

Closed
nikkiselev opened this issue Jan 18, 2020 · 27 comments · Fixed by #11901
Closed

Inconsistent loading order of CSS Modules + global CSS #10148

nikkiselev opened this issue Jan 18, 2020 · 27 comments · Fixed by #11901
Milestone

Comments

@nikkiselev
Copy link

nikkiselev commented Jan 18, 2020

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.

@Timer Timer added this to the 9.2.1 milestone Jan 19, 2020
@Timer
Copy link
Member

Timer commented Jan 19, 2020

Please provide an example of the CSS files used.

@nikkiselev
Copy link
Author

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

@liborvanek
Copy link

Once this issue is resolved, will there be any way to control the order of imported CSS files?
Looks like the default order is global CSS first, CSS modules afterwards.
Frem previous comments I understood it wasn't possible to modify CSS import order when using next-css, but it might be possible using new CSS Support. But I can't seem to find any mention neither in the RFC, docs nor release notes.

A common desirable pattern might look like this:

– resets
– global CSS (typography etc.)
– CSS modules
– global utilities

@timneutkens
Copy link
Member

CSS resets are global CSS, you can import them first in _app.js and it'll be loaded in that order.

// in pages/_app.js
import 'reset.css'
import 'global.css'

@liborvanek
Copy link

@timneutkens Yes, I know. Sorry for not being clear. My question is more like: Are we able to import CSS, that would be loaded after CSS modules?

@timneutkens
Copy link
Member

You can't know the load order of CSS modules as they get added per-page. So there really is no "after" CSS modules state.

@liborvanek
Copy link

@timneutkens I don't need to know the load order of CSS modules, I just need to find a way to include a global CSS at the very end of <head>, so dynamically loaded CSS modules are injected always before this stylesheet, not after.
Does that make sense? Is it even possible?

@Timer
Copy link
Member

Timer commented Jan 20, 2020

@liborvanek that is not possible, nor a use case we're currently interested in exploring. CSS will be loaded the order it's imported by the application.

@Exelord
Copy link

Exelord commented Feb 28, 2020

Any update on this issue? It's blocking my styles development using sass :/

@pav-w
Copy link

pav-w commented Mar 10, 2020

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...

@Timer Timer modified the milestones: 9.3.x, 9.3.1, 9.3.2 Mar 12, 2020
@bravokiloecho
Copy link

bravokiloecho commented Mar 16, 2020

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?

@valse
Copy link
Contributor

valse commented Mar 18, 2020

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

@Timer Timer modified the milestones: 9.3.2, 9.3.3 Mar 27, 2020
@Timer Timer modified the milestones: 9.3.3, 9.3.4 Mar 30, 2020
@jahirfiquitiva
Copy link

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

@Timer Timer modified the milestones: 9.3.4, 9.3.5 Apr 3, 2020
@jrobchin
Copy link

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

@sparkertime
Copy link
Contributor

sparkertime commented Apr 10, 2020

I too am suffering from this. Right now my only solution is to switch to cycles of yarn build && yarn start when working on CSS. This is also fixed by reverting to 9.1.7 and using the next-css / next-sass plugins, but that leaves a lot of great recent features on the table. I was hoping I could leverage the composes feature of css-modules to work around this but I think that's blocked by a separate issue in #10142

I took a look at the CSS loading code in next as well as what magic happens in yarn build and it's beyond me, but I really hope this gets a PR soon.

(fixed, see below)

@sparkertime
Copy link
Contributor

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!)

@jrobchin
Copy link

@sparkertime this is awesome, thanks!

@Joozty
Copy link

Joozty commented Apr 16, 2020

@sparkertime many thanks for looking into it. Nonetheless, it's still broken when I import some components in _app.jsx. I prepared a repo to reproduce. Hope it helps :)

https://github.com/Joozty/nextjs-css

@sparkertime
Copy link
Contributor

@Joozty You'll probably want to open an issue specifically for ordering when importing modules from _app.jsx. This issue is now closed and what you're reporting is definitely beyond my ability to fix, so hopefully a new issue will get some attention from the core team

@chornos13
Copy link
Contributor

chornos13 commented May 31, 2020

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

@chornos13
Copy link
Contributor

after finding out what is the problem, I found that this issue because of mini-css plugin, I tried with my own version of webpack-config, after I added mini-css plugin to build on production this bug happened.

@typeofweb
Copy link
Contributor

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.

@millievn
Copy link

millievn commented Jun 18, 2020

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

same problem. I use reset.scss in _app.tsx and it does not work unless i reload page. Any solution now?

@chshanovskiy
Copy link

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";

@hydRAnger
Copy link
Contributor

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

@bigtimeyash
Copy link

Not really a fix, but a temporary solution is to directly copy and paste the files into one document to prevent Next from re-ordering them.

I am on Next v9.5.2. This is preventing me from using next for future projects. Could you please advise when this will be fixed? Or is there a way to specify the order of imports for css and scss files?

@Timer
Copy link
Member

Timer commented Aug 12, 2020

I'm locking this issue because the original issue was solved. If you are experiencing a bug, please open a new issue.

@vercel vercel locked as resolved and limited conversation to collaborators Aug 12, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.