nextjs - 💡(How to fix) Fix Dynamic import of SSR components is causing the CSS of all components to be loaded [4 comments, 1 participants]

Official PRs (…)
ON THIS PAGE

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#86777Fetched 2026-04-08 02:09:22
View on GitHub
Comments
4
Participants
1
Timeline
12
Reactions
1
Participants
Timeline (top)
commented ×4subscribed ×4issue_type_added ×1labeled ×1

Root Cause

The first one is okay because it's just all the Tailwind CSS classes and configurations. But the second one is not, because on this page I only imported the Button component (via the dynamic import component, as you can see here app/products/[id]/page.tsx), and what I see is the CSS for all of my components:

Code Example

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 32286
  Available CPU cores: 20
Binaries:
  Node: 22.14.0
  npm: 10.9.2
  Yarn: 1.22.22
  pnpm: 10.24.0
Relevant Packages:
  next: 16.0.6 // There is a newer version (16.0.7) available, upgrade recommended!
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A

---

import SectionA from '@/components/section-a'

export const dynamicSections = {
  sectionA: SectionA
}

// or:

export const DynamicSections({ type }: { type: string }) => {
  switch (type) {
    case 'sectionA':
      return <SectionA />;
  }
}
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/pferreirafabricio/nextjs-bug-css-modules

To Reproduce

We can reproduce the issue in 2 ways:

Development mode

  1. Clone the project
  2. Run npm install
  3. Start the dev server with: npm run dev

With production build

  1. Clone the project
  2. Run npm install
  3. Build the app: npm run build
  4. Run the app from the build: npm run start

Current vs. Expected behavior

Context: I have a project with its own sections dynamically loaded from a CMS. Each page has a structure defined by a user, and we render it with Next.js based on that structure.

So what I wanted to do is, based on that array of sections, dynamically get the component and render it, but this is not working as expected, since all the CSS and the JS of these components are being loaded in every page, even if the page doesn't use that actual component/section.

Therefore, the expected behaviour is that only the CSS of the components being used in the page is loaded.


In development mode, if you open the page http://localhost:3000/products/1 you will see that 2 CSS files are loaded:

<img width="1909" height="315" alt="Image" src="https://github.com/user-attachments/assets/46971a4f-8494-4324-82a7-9a02d5566300" />

The first one is okay because it's just all the Tailwind CSS classes and configurations. But the second one is not, because on this page I only imported the Button component (via the dynamic import component, as you can see here app/products/[id]/page.tsx), and what I see is the CSS for all of my components:

<img width="1902" height="1108" alt="Image" src="https://github.com/user-attachments/assets/2ac27af7-ecdb-4b64-8e2b-91d055890bd4" />

Now, if you open the http://localhost:3000/blog/1 you will see 2 files as well.

<img width="1914" height="309" alt="Image" src="https://github.com/user-attachments/assets/9fe18c25-0515-4c1b-b5fb-3f471db402a1" />

The first one is okay too, because it's from Tailwind CSS. However, on this page, I have only the CSS from the components that I've imported in this page (in this case, a custom Link component, as you can see here app/blog/[slug]/page.tsx):

<img width="1908" height="600" alt="Image" src="https://github.com/user-attachments/assets/ce206a5c-5963-4225-8fc9-5c72e13a907f" />

When running with a production build, the scenario is even worse, because now, even on the blog page, all the CSS of all components is being loaded:

<img width="1914" height="496" alt="Image" src="https://github.com/user-attachments/assets/2a728723-ca90-41d9-9cde-d13fb790e60b" /> <img width="1920" height="827" alt="Image" src="https://github.com/user-attachments/assets/bf7a2734-7fa7-46dd-af35-89ee272a25cb" />

This same behaviour happens with both of the pages, blog and product.

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Pro
  Available memory (MB): 32286
  Available CPU cores: 20
Binaries:
  Node: 22.14.0
  npm: 10.9.2
  Yarn: 1.22.22
  pnpm: 10.24.0
Relevant Packages:
  next: 16.0.6 // There is a newer version (16.0.7) available, upgrade recommended!
  eslint-config-next: N/A
  react: 19.2.0
  react-dom: 19.2.0
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

CSS, Lazy Loading, Performance, Turbopack

Which stage(s) are affected? (Select all that apply)

next dev (local), next build (local), Vercel (Deployed)

Additional context

I'm using Tailwind CSS with CSS modules to do the styling.


I already tried a variety of approaches for the dynamic-section component, but all of them resulted in the same weird behaviour, including:

  • Use switch cases instead of an object
  • Separate the dynamic section component into 2 parts: client-registry and server-registry, where the client-registry uses dynamic(() => import()) and the server-registry uses only () => import()
  • Importing all the components directly at the top of the file and just returning them in a switch case, something like:
import SectionA from '@/components/section-a'

export const dynamicSections = {
  sectionA: SectionA
}

// or:

export const DynamicSections({ type }: { type: string }) => {
  switch (type) {
    case 'sectionA':
      return <SectionA />;
  }
}

There is a Reddit thread that I think is related to this topic: Problem with app router no-one is talking about - Dynamic Loading

extent analysis

TL;DR

The issue can be addressed by utilizing Next.js' built-in support for dynamic imports with CSS modules, potentially requiring adjustments to the dynamic section component and leveraging next/dynamic with ssr: false to ensure correct loading of CSS.

Guidance

  • Review the usage of next/dynamic for dynamic imports, ensuring that ssr: false is used where applicable to prevent server-side rendering of unnecessary components.
  • Consider implementing a more granular approach to loading CSS modules, potentially by utilizing a library or custom solution that can handle dynamic loading of CSS based on the components being rendered.
  • Investigate the impact of upgrading to the latest version of Next.js (16.0.7) as recommended, which may include fixes or improvements related to dynamic imports and CSS loading.
  • Analyze the Reddit thread provided for potential insights or workarounds related to dynamic loading issues with the app router.

Example

import dynamic from 'next/dynamic';

const DynamicSection = dynamic(() => import('@/components/DynamicSection'), {
  ssr: false, // Ensure this is set to false for client-side rendering only
});

// Usage within the component
<DynamicSection />

Notes

The provided information suggests that the issue is related to how Next.js handles dynamic imports and CSS modules. However, without direct access to the codebase, it's challenging to provide a definitive solution. The guidance offered is based on common practices for managing dynamic imports and CSS loading in Next.js applications.

Recommendation

Apply a workaround by utilizing next/dynamic with ssr: false for client-side rendering of dynamic sections, and consider upgrading to the latest version of Next.js (16.0.7) for potential fixes related to dynamic imports and CSS loading.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING