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.

Styles duplicated across css chunks create source order issues

See original GitHub issue

Bug report

Describe the bug

I have an application that uses a component on multiple pages. When I build the project that component’s styles are duplicated in each of the relevant css page chunks; this creates visual bugs.

For example (some-component, and initial-page-component-override are classes applied to the same element in the DOM on the initial page):

initial-page.chunk.css

// component styles
.some-component { margin-bottom: 10px; }

// page specific override
.initial-page-component-override { margin-bottom: 20px; }

second-page.chunk.css

// component styles
.some-component { margin-bottom: 10px; }

When second-page.chunk.css is added to the DOM the component styles are reapplied over the top of any page specific styles defined in the initial page. The overrides are lost and the incorrect margin is now applied to the component on the initial page. N.B. this is not an issue in dev mode, only in production.

To Reproduce

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

  1. Checkout https://github.com/petewarman/nextjs-css-module-issue
  2. npm i, npm run build, npm start
  3. Hover the cursor over the lime green button
  4. See it change to pink when the second page’s stylesheet is preloaded

Expected behavior

Style declarations should not be duplicated in compiled stylesheets. Component styles should loaded (in their own chunk?) above page specific styles.

System information

  • OS: macOS
  • Version of Next.js: 9.3.6
  • Version of Node.js: 10.16.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:26
  • Comments:42 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
VictorPivnenkocommented, Aug 6, 2020

Experienced the same issues with next v9.5.1 on prod build, on dev build the styles are ok.

8reactions
iksentcommented, Apr 30, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Organizing your CSS - Learn web development | MDN
If you create very specific selectors, you will often find that you need to duplicate chunks of your CSS to apply the same...
Read more >
Route-based code splitting causes css duplicate
Using route-based code splitting causes css duplicate in chunks. ... with style overwriting; Run build command; Check static folder.
Read more >
mini-css-extract-plugin - npm
This plugin extracts CSS into separate files. It creates a CSS file per JS file which contains CSS. It supports On-Demand-Loading of CSS...
Read more >
Refactoring CSS: Introduction (Part 1) - Smashing Magazine
CSS refactoring is not an easy task — it needs to be done in a way that doesn't create problems. First we need...
Read more >
Separating CSS - SurviveJS
extract-css-chunks-webpack-plugin is a community maintained alternative to ... If you want strict control over the ordering, you can set up a single CSS...
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