-
Notifications
You must be signed in to change notification settings - Fork 27k
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 CSS resolution order #64921
Comments
It seems |
@GabenGar Thanks for sharing a |
We are seeing the same issue on The specific example we are seeing is that CSS styles imported into layout.js are overriding CSS styles set in a component level stylesheet even though they have the same CSS specificity and the component CSS should override the layout's CSS. This bug appears to be due to the order that the CSS is included in the final static .css files that are included in the production build. |
@benjitastic What kind of CSS styling are you using in this case (e.g., css modules)? |
No, not modules. Just like this inside the component:
Some more details: In this case layout.js had this at the top: And customTheme.scss had this inside it: That bootstrap file has a css style declared for .btn like this: Then in the component we have an element The expectation is that .filter-pill padding can override .btn padding. But .btn was overriding .filter-pill styles. This was because of the 5 Hard to post a repro since I think you need a project that has enough CSS to result in multiple static CSS files being generated. We reverted back to 14.1.4 and the CSS went back to the correct order. |
I am seeing this issue as well, particularly for global styles as well as styles using css modules. As mentioned in this issue, it only happens in production builds. |
Setting |
Interesting -- I can't find any docs anywhere on the Does anybody know exactly what "strict" css chunking does? |
It does not seem to resolve the issue for us :( |
This comment has been minimized.
This comment has been minimized.
@Netail Thanks for sharing.
I can confirm there are several broken cases with the ordering of CSS, after looking at several
Getting some answers internally to further clarify this—will respond back soon! |
That would be great. We use a design system package and a navigation package which uses the design system package (with some overrides) and the app using the design system, but the overwrites are currently not working on productions. Thus making NextJS kind of unusable currently for us. So the sooner the better 😅 Do you by any chance have a ETA when development on this will happen? |
Can confirm this issue also comes up in a project using Next.js 14.2., Mantine v7.10 components, and css modules. Works fine in development mode, loads incorrectly in production. |
I had a similar issue, where global styles were bundled after component styles. Running dev I never had an issue, only on production. I'm using Next 14.2.2 with App router and SSG. My workaround is only for getting global scss that's imported in layout.js to load ahead of client component scss modules. But perhaps this will be helpful for someone else / debugging the overall issue. In my root layout.js I was importing /global.scss There is an Indeed there was css rules added above the global.scss. After analyzing it seems that the css above my My workaround fix is to make a new client component that imports the styles "use client";
import "@/scss/global.scss";
const GlobalStyles = () => {
return <></>;
};
export default GlobalStyles; and then import that component in my root layout.js import GlobalStyles from "./GlobalStyles";
export default function RootLayout({ children }) {
return (
<html lang="en">
<GlobalStyles />
<body>
{children}
</body>
</html>
);
} This resolved the issues I was getting in dev tools, and also some issues with specificity (component styles were no longer overriding global styles before the workaround). |
@Netail No ETA to share yet, but this issue is definitely high on our plate! |
I noticed that if I replace I believe this regression was introduced here as part of the 14.2 release. Edit: It appears that removing the sorting also results in the correct order |
@piratetaco Is it possible to @michaelkostal Can you try testing a later Next.js version? I believe this |
@samcx I face this issue on 14.2.3 as well. Can we backport the additional bug fix to v14 as well for those who cannot switch to a canary version or upgrade a major version at this time? |
@paulyi The latest changes should now be in 14.2.5 (includes both fixes mentioned above). |
@samcx just tried out the 14.2.5 release -- while this release is an improvement, I'm still seeing some incorrect loading of css in the production environment. |
Can you describe exactly how it's loading incorrectly, and is it possible to provide a minimal, public |
It'll take me a bit to build a public repro for you, but I'll see if I have some time this weekend. The project I'm working on uses Mantine UI for our component library, which has a base styles css file that must be loaded first. Those are imported at the top of our |
@samcx upgrading to 14.2.5 did resolve my issue. It now appears my global styles are loaded first in order as expected. Thanks! |
@mrabuse @michaelkostal That's great to hear! |
sadly I'm unable to get a public repro going. The issue only seems to appear after the components are mixing and matching through the entire component library we're maintaining - but the issue we are seeing is still happening as of 14.2.5. For now we are going to import higher stylesheets in the component.js in our library as a cumbersome workaround.
|
The issue still persist as of 14.2.5 version, I'm using Sass with CSS Modules and I get inconsistent css import order between running the dev server locally and the production build, my app is quite big and I cannot get you a public repo up. Also this doesn't happen on small projects where only 1 chunk of css is build, in my case I have 5/6 chunks of css being build and I can't really reproduce something of that magnitude. In this case its a composed component from an atom where I want to overwrite the gap, locally all works as expected but when building the project the css imported chunks order is being mixed. My only solution at the moment is the one suggested by @piratetaco but I have a huge project, please fix this! |
+1 on this only surfacing once your app generates multiple chunk files. Your explanation of the issue matches exactly the symptoms we reported back in May. I'm still sitting at v14.1.4 and we are waiting to upgrade until this has been resolved, we are not yet considering implementing any work-arounds described in this issue thread as they are cumbersome to implement and maintain. |
@samcx, I've noticed that my js still gets tree shaken (likely given that my library is included in the transpile modules pattern), although there might be some slight adverse build time impact from removing it. However, while the CSS is now all in order, it no longer treeshakes to only include the styles for the modules actually used. (I can create a quick replication for this if helpful?) In my case maintaining a legacy library, this is about 100kb of css built (which zips much much smaller) but still a fairly significant consideration. |
We've tried adding I guess Next's CSS bundler has a sideEffect itself, so, if it works incorrectly, ignoring it might be a solution to the problem Regarding of changes in the bundles:
|
@ORLVNIMUSIC that seems like one of the the duplication issues (two) rather than the order issue? Are you on next 14.2.5(+)? As of 14.2.5 I'm no longer able to replicate the duplication in chunks for the same page issue, even with a minimal repro |
@piratetaco it doesnt seem so as we've problem neither with global styles, nor styles from last page (styles are out of order right after first app load) we are on Its just that styles for a single element occur in different files (aka style tags) when there are to much of CSS for a page, and the second file (tag) appears to have a priority over the first (regardless of import order, applying order etc) I believe the reason of wrong style order resolution (for our case at least) is bundler optimization, as Next tries to create multiple CSS files of the same size, instead of one very big file (but i might be completely wrong about it) ps. I've checked if deleting some css would help to resolve the problem, and it did! (just created one CSS file for production instead of two) Also adding |
enabling the --turbo flag helped me on the version |
@samcx Is this fixed? We are facing some nasty memory leak issues and desperately need to upgrade to 14.2.5. We have a throughput of 10k requests/minute (minimum) and we need a workaround asap, is there any update? Please let me know incase of any possible solutions. |
@rohitpotato If you have memory issues, it could be related to your Node version. Please make sure to report on a related-issue, as this issue is about CSS-ordering. |
HI @samcx, thanks for the quick response. This is not related to our node version. I am talking about the memory usage issue mentioned in the official 14.2 release, we were facing the same issue and 14.2 claims to resolve that, and thats why we are trying to upgrade, resulting in the above issue with incorrect CSS ordering. Also, can you point me to the node version issue if thats the case? Would like to investigate more. Current config:
|
@rohitpotato This is the separate issue I found that was causing memory leak issues, which stemmed from a Node.js release. |
Just as a tiny bit of extra gas here, I took my repro and swapped the nextjs version from 14.2.5 to 14.1.4 and you can see that the library styles emit properly again, even with Will edit this in a minute to see if I can isolate to being introduced in a specific version in 14.2 or not. Edit 1: Starts in 14.2.0 flat. https://github.com/vercel/next.js/releases/tag/v14.2.0 Edit 2: Issue remains in 14.2.7. Cloning next and going to try some reverts of some sussy merges to try and isolate the specific change. |
@rohitpotato Sorry I forgot to include the link to it! → #68636 |
Dear Next.js team, I’ve just hit this issue on 14.2.8. It’s frustrating that 14.1.4 still seems to be the last stable 'golden' version for reliable CSS ordering. I've lost track of how many times I've tried upgrading to the latest Next.js. First, it was the environment variable issue, which finally seemed to be resolved, but now it's this. My project has been stuck on 14.1.4 for over six months, and it's becoming a blocker. Any updates on when we can expect a real fix? |
@jianliao There's been multiple fixes since the start of this thread (and similar ones), but yet there are still some edge cases that haven't been solved.
I don't think this is really true. There were other cases that were fixed from the first fix that updated the behavior, but afterwards, there were still other cases that needed fixing. If you want to see progress with whichever specific CSS issue you're coming across, I would first start with sharing a minimal |
Hi Team, We have a similar issue. Our next.js (v14.2.10) project is using SCSS modules (like page.modules.scss) as well as a globas.css file imported in the root layout. Our application appears significantly different in production compared to the development server, due to inconsistencies in the order of CSS rules. This discrepancy is a deal breaker for us, as it makes it unreliable to go live. I appreciate any suggestions or updates on resolving this issue. Thanks |
I have isolated at least my issue to this commit released in 14.2.0. If I revert this, css emits in the correct order from a library with or without sideEffects: false set. Looking into why. I have isolated library css improper order to the changes in this file. For CSS files, I don't believe it is a good practice to restrict to only |
@piratetaco @samcx what is the expected timeline for backporting this fix to v14? We can't upgrade our Next.js version to fix the cache-poisoning security vulnerability until this is backported because it would destroy our production build interface for our users. |
The PR hasn't merged yet, and we are looking to release another v15 soon. So not entirely sure yet if the aforementioned PR if this will be backported, but not out of the picture!
Not sure what you are referring to here |
15 CSS fix PRs?! How come so many? |
@samcx, your team found a high level security vulnerability around cache-poisoning -- you can read the announcement about the problem, the users it impacts, and the fix here. The fix was released on v14.2.10. The problem for every team impacted by the css rendering order bug is that we cannot upgrade Next.js past v14.1.4 without breaking our production renders. So now we all have to make a choice between staying on v14.1.4 and living with a known security vulnerability until v15 is officially released, or, when @piratetaco's fix gets merged into the v15 canary, upgrading to the unstable canary version to fix both the css rendering issue and security vulnerability, but having to discover the impacts of the other officially unstable features on our applications. For those reasons, it would be appreciated if when @piratetaco's fix is approved, you could push for that change to also be backported to v14 quickly. @Netail I believe he means a v15 release, not 15 PRs. |
I meant Next.js version 15! |
@mrabuse thank you for sharing. To clarify, the cache-poisoning vulnerability is only for Pages Router along with two other requirements. @piratetaco's PR is to fix a CSS issue with App Router. If you're on Pages Router, there shouldn't be any issues with the ordering of your CSS (as far as I'm aware) on the latest stable v14. |
@samcx it is technically possible to use both routers in one Next.js application, so it is possible for a team to be impacted by both issues. |
@samcx, My team has built an app-router NextJS app but in order to integrate with a 3rd-party auth SDK we needed to include some pages routes, so we're technically a hybrid, and therefore we are stuck with both issues simultaneously. |
The best of both worlds, some would say. |
Faced with this issue on 14.2.4 |
Encountered this issue in production mode on v14.2.15 using the Pages Router and a custom Webpack 5 config with Mini CSS Extract plugin. The order was fine in development mode. The issue was due to two pages (A and B) importing and rendering the same Page Template component. The page template imports a Link component and overrides its styles using css modules, while one of the pages (A) also imports the same Link component but does not alter its styling. PageTemplate.js
pages/page-a/index.js
pages/page-b/index.js
The shared Page Template styles were ending up in a separate stylesheet, which was added to the
The solution was to extend the existing cache group config in next.config.js webpack config to put styles in a separate cache group:
After the change the stylesheets are added to |
Next.js 14 and Bootstrap CSS Override Issue (solved for me)I was using Next.js 14.2.14 and Bootstrap 5.3.0 for responsive design, along with custom CSS to style the app. I ran into an issue where Bootstrap was overriding my custom styles in the production build, though everything worked perfectly in the dev server. Problem SummaryAfter trying, I found the following:
My Initial SetupMy /* styles/global.css */
@import url('/assets/css/style.css');
@import url('/assets/css/responsive.css');
@import url('/assets/css/magnific-popup.css'); And my // pages/_app.js
import Head from 'next/head';
import 'bootstrap/dist/css/bootstrap.min.css'; // Bootstrap CSS
import '@/styles/global.css'; // Custom global styles The SolutionI merged all the individual CSS files into a single After Fix:I combined all the styles from /* styles/globals.css */
/* Contents from style.css, responsive.css, and magnific-popup.css merged here */ This approach solved the issue and made sure my custom styles worked in production. You can try merging all CSS files into one to avoid issues caused by CSS ordering in Next.js. |
Link to the code that reproduces this issue
https://github.com/GabenGar/repros/blob/main/nextjs/css-out-of-order/README.md
To Reproduce
Reproduction steps are in the README.md
Current vs. Expected behavior
Current:
Different CSS resolution order between development and production. Before I had weird client vs. render CSS issues, but it looks like they are fixed in 14.2, although they weren't super reproducible before either.
Expected:
Work basically like
pages
router.Provide environment information
Which area(s) are affected? (Select all that apply)
Not sure
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local), next start (local), Vercel (Deployed)
Additional context
No response
The text was updated successfully, but these errors were encountered: