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

CSS modules duplicated when used with next/dynamic #25456

Open
mrmckeb opened this issue May 25, 2021 · 28 comments
Open

CSS modules duplicated when used with next/dynamic #25456

mrmckeb opened this issue May 25, 2021 · 28 comments
Assignees
Labels
bug Issue was opened via the bug report template. Lazy Loading Related to Next.js Lazy Loading (e.g., `next/dynamic` or `React.lazy`). Webpack Related to Webpack with Next.js.

Comments

@mrmckeb
Copy link
Contributor

mrmckeb commented May 25, 2021

What version of Next.js are you using?

10.2.2

What version of Node.js are you using?

14.16.0

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

When using next/dynamic with CSS modules, we've found that CSS can be re-inserted (duplicated)

In our scenario, we have a simple <Text /> component that is already in use throughout the application. When we used <Text /> in a new component that was dynamically imported, styles for <Text /> were re-inserted.

These styles were inserted below all other styles, breaking some styles that rely on specificity - which is how we found this bug.

As a temporary workaround, we've removed that component from the dynamically loaded component.

Expected Behavior

Styles should not be re-inserted/duplicated.

To Reproduce

We haven't been able to reliably reproduce this in a standalone repo.

We've looked and found some potentially similar issues, and this might be related: #20203.

@mrmckeb mrmckeb added the bug Issue was opened via the bug report template. label May 25, 2021
@ijjk ijjk added kind: bug and removed bug Issue was opened via the bug report template. labels May 25, 2021
@ijjk ijjk added this to the Iteration 21 milestone May 25, 2021
@ijjk ijjk assigned sokra and ijjk May 25, 2021
@sgehrman
Copy link

I'm getting duplicate css without using dynamic.

@timneutkens
Copy link
Member

@sgehrman please provide a reproduction

@bscaspar
Copy link

bscaspar commented Nov 1, 2021

I am encountering a similar issue - CSS modules are getting duplicated and overwritten for similar components on a page - e.g. implementations of a custom <Button /> component - after one of these components is dynamically loaded. No issue when the conditional dynamic component is not loaded. There's a clear flash of changing styles once the dynamic component is loaded.

Update: was initially on next@11.1.2, updating to v12.0.2 didn't fix this issue.

@styfle styfle modified the milestones: 11.1.x, 12.0.4 Nov 5, 2021
@timneutkens timneutkens added Webpack Related to Webpack with Next.js. Lazy Loading Related to Next.js Lazy Loading (e.g., `next/dynamic` or `React.lazy`). labels Nov 16, 2021
@timneutkens timneutkens removed this from the 12.0.5 milestone Nov 17, 2021
@mrmckeb mrmckeb closed this as completed Jan 6, 2022
@mrmckeb mrmckeb reopened this Jan 6, 2022
@vborodulin

This comment was marked as off-topic.

@vborodulin

This comment was marked as off-topic.

@rikani
Copy link

rikani commented Mar 9, 2022

Next.js v12.1.0 still has problem.

@Puvvl
Copy link

Puvvl commented Apr 12, 2022

Next.js 12.1.4 still has problem

@ycfreeman
Copy link

12.1.6 still has problem

@PhilipeGatis
Copy link

12.1.5 still has problem

@NikooDev
Copy link

NikooDev commented Jun 8, 2022

12.1.6 still has problem :
Capture d’écran 2022-06-09 à 00 04 51

@PepeBotella25
Copy link

PepeBotella25 commented Jul 4, 2022

Repo to reproduce this:
https://github.com/PepeBotella25/css_module_dynamic_issue

Initially
image

After adding dynamic component using the same "shared" component
image

Since you need css chunks to reproduce it you have to do build+start

npm run build
npm run start

When second component get added its css brings the "shared" styles again affecting the cascade order:
image

@X7Becka
Copy link
Contributor

X7Becka commented Jul 30, 2022

Yeah, 12.2.3 still no workaround

@yakovlev-alexey
Copy link

yakovlev-alexey commented Dec 26, 2022

Experienced this issue in 12.3.1 and found a workaround.

TL;DR: add a hidden (display: none should work) div in App/Page. In this div render all the components that have their styles duplicated.


Explanation: webpack (or whatever) seems to check whether components used in a dynamic import are already present on the page. In my case everything was fine when a loadable component was imported on a specific page. Later I moved the component to _app.tsx and styles started to duplicate in the CSS file emitted for the loadable component.

Say you have a few components:

  • App (pages/_app.tsx)
  • Page (pages/index.tsx)
  • Button (components/button.tsx)
  • LoadableModal (components/loadable-modal.tsx)
  • LoadableModal uses Button

* uses means imports either directly or indirectly but without dynamic imports.

The issue may be reproduced in a few ways:

  1. LoadableModal is used in App, Button is used in Page (sure, tested)
  2. LoadableModal is used in Page, Button is used in App (not sure, need to test)
  3. Button is used neither in Page nor in App. There is an additional LoadableForm component that uses Button used on the same page. (not sure, need to test)

When building Next.js checks imports to see whether components used in dynamic are already present on the page. Since the relation between Page and App is not direct there is no way for it to detect that the Button is already used.

My workaround was the following: add a hidden (display: none should work) div in App/Page. In this div render all the components that have their styles duplicated. With this fix in place Next.js should properly deduplicate styles.

Perhaps it is also possible to not render the component at all by using {false && <Button />}.

@aloeugene
Copy link

We're also experiencing this with Next@13.1.5.

image

And it's not reproducing running locally with next dev, which makes it even worse given the fact that we need to double-check such issues by building "prod" version locally OR just observing it when it gets to QA env (which uses "prod" version).

@uaeio
Copy link

uaeio commented Mar 22, 2023

It seems that this issue is connected with: #42082

@Webbrother
Copy link

The same issue without using dynamic.
next@13.2.4

I'm wondering why this problem persists for a few years and a lot of tickets was closed without any solution.

@potapovnikita
Copy link

any solutions for this issue?

@vasylkorchynskyi
Copy link

Any updates here? Have the same issue with next@13.4.7

image

@AustinCase
Copy link

I am also having this issue with next@13.4.5 -- I am actively trying to work on reproduction, perhaps will lend itself to the issue.

My instinct is that it has to do with SSR and the bundling magic between layouts/pages...

@roman-veryovkin
Copy link

I have the same issue with next@13.4.17. Any workarounds so far?

@AustinCase
Copy link

I can actively reproduce it without dynamic.

  • Create a shared component outside of app directory.
  • Create two or more pages that utilize the shared component.
  • Notice that the shared component has duplicate styles.

Even if you have not visited both pages, if you have a Link component ( or something else that preloads the other page ). The server will stream down two css files, both with the EXCACT same selectors.

The browser for good reason can't dedup css selectors, instead it essentially does a merge. Ultimately I am just worried about the performance hit here, since it is doing the dedup on basically every shared component. Its also messy to read and a bad dev experience.

Hope this helps :)

@ForsyteRun

This comment has been minimized.

@monolithed
Copy link

This bug is present even without using next/dynamic

@Egor-Dubovik
Copy link

I have the same issue in next@13.5.6 even without dynamic imports. Seriously this problem has been going on for so long and it still hasn't been fixed?

chatonode added a commit to chatonode/property-dev-notifier that referenced this issue Feb 2, 2024
…d of PageTitleWrapper. Also cancelled cache wrapping for getPropertyDevelopers to get fresh list each time user needs it. Also re-activated redirecting to the Home page when clicked on Sidebar logo.

Related ongoing next.js bug of multiple css module files for the Sidebar(client component) rendered inside layout(server component)
- @ref-link: vercel/next.js#25456
@zachsitka
Copy link

zachsitka commented Mar 13, 2024

This is still an issue for me using next@14.1.3

EDIT: using dynamic imports

@maiconsanson
Copy link

maiconsanson commented Mar 13, 2024

It seems to be solved in 14.1.3 without dynamic imports (production only, dev still duplicating) for me.

#61198

@ivangorbunoff
Copy link

ivangorbunoff commented Apr 8, 2024

is there any solution? In large projects where components are often reused, duplicating styles greatly increases the final css file size.
This is still an issue for me using next@14.1.4

@samcx samcx added bug Issue was opened via the bug report template. and removed kind: bug labels Apr 30, 2024
@lalexgap
Copy link

lalexgap commented Aug 1, 2024

We still see duplicate CSS for dynamic imports on 14.2.3 in our production builds. It would be great to have a fix for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Lazy Loading Related to Next.js Lazy Loading (e.g., `next/dynamic` or `React.lazy`). Webpack Related to Webpack with Next.js.
Projects
None yet
Development

No branches or pull requests