-
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
CSS Modules sometimes don't load when using next/dynamic import #33286
Comments
Hey, wondering if there are any insights or leads on this one! It's affecting us in production and we don't have any known workarounds — next/dynamic is required for to avoid a problematic library import that is not SSR-compatible. Thanks! |
Having similar issues with missing css files! |
bump, same issue on nextjs 12.1.5 |
this looks similar to #17464 , which is about nextJs removing preloaded css. there're some solutions to solve this issue, but kind of hacky way. |
Also seeing this on NextJS 12.2.2. Can confirm that the issue goes away if not using dynamic imports. |
The fix mentioned in #17464 has worked for me as a temporary workaround:
|
the issue persist on next 12.2.4 and was not a thing for a long time and suddenly appeared. |
This issue is happened in AppDir beta version of NextJS 13 as well. |
Having the same Issue right now using NextJS 13. Any News here ? |
Having the same issue with Next.js 13, when navigating to any other page from the Home page, the CSS doesn't work. |
Any lead on this so far, or any workarounds? It seems that it only happens on production after deployment for me. |
In my Case, im building a Site with Data coming from a CMS. So i wanted to load the needed Components to the currently active Sites with Next/dynamic. I tried all kind of stuff, but in the End solved the Issue with a switch. Guess we have to wait for a future release to get things fixed with dynamic. For now i would only recommend to use it with small components that dont load extra CSS. Heres an Example how i did it, like this the Components will be returned the normal way and CSS Modules will be loaded:
|
Same issue here :( v.13.4.3 |
Same issue in 13.4.4 |
…gating from home
opened repro scenario here #50300 |
Same issue with next.js 13.4.7 |
A possible workaround for this problem is to move all async chunks into one group.
export default {
webpack(config, context) {
const { isServer, dev } = context
if(!isServer && !dev) {
config.optimization.splitChunks.cacheGroups.asyncChunks = {
enforce: true,
type: "css/mini-extract",
chunks: 'async',
}
}
return config
}
} |
Same issue here next.js v 13.4.19 |
Has anyone had luck with this solution? I have not tried it yet in production but it seems promising locally when removing |
It does not work for me.
where
it works |
This is still an open issue for us ! |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I’ve been looking into this issue for quite a while and found that my case is exactly like the one described in this GitHub issue. It’s a bit surprising (and a little frustrating) that CSS modules aren’t fully usable yet. I came up with a workaround to fetch styles dynamically, and I thought I’d share it here in case it helps someone else: import dynamic from 'next/dynamic';
import React from 'react';
const StyleProvider = dynamic(
() =>
import('styles/FancyStyles.module.css').then((module) => {
interface ComponentProps {
children: (style: typeof module.default) => JSX.Element;
}
const Component: React.FC<ComponentProps> = ({ children }) =>
children(module.default);
return Component;
}),
{
loading: () => <p>Loading...</p>,
}
); const FancyComponent: React.FC = () => (
<StyleProvider>
{(style) => <SomeComponent className={style.fancy} />}
</StyleProvider>
); This solution works for now, but it definitely feels more like a stopgap than a permanent fix. If anyone has a better approach or any insights on how to handle this more effectively, I’d really appreciate your input! |
Run
next info
(available from version 12.0.8 and up)What version of Next.js are you using?
12.0.8
What version of Node.js are you using?
14.17
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
Vercel
Describe the Bug
When loading a component using
next/dynamic
, CSS modules are sometimes not loaded correctly. This seems like it may be similar to #18252, which is described as fixed (although there is no linked PR showing the fix).This bug only occurs when running
next build
+next start
. Works as expected in development (next dev
). Additionally, there seem to very specific scenarios where it occurs, which I've tried to outline as clearly and concisely as possible int he repro steps.Expected Behavior
CSS modules should be loaded into the document so that your styles are applied.
To Reproduce
Repro Repo and Deployment
I've created a demo using
create-next-app --typescript
12.0.8: https://github.com/jzxhuang/dynamic-import-css-module-next-bugIt's hosted at: https://dynamic-import-css-module-next-bug.vercel.app/
Isolated Reproduction Steps
/foo
. The button will now have a clear background, even though it is the exact same component with no changes whatsoever.This can be reproduced with both SCSS and CSS modules. Repeating the above steps with https://dynamic-import-css-module-next-bug.vercel.app/broken/two will break the blue button. This is reproduced only in the "production" build, if you follow the same steps with
next dev
, the buttons on/foo
should always be the correct color.Cases where it works, but why?
The above steps are an isolated reproduction of this bug which we found in production. Now, here's where it starts getting super weird, we decided to do some more experimentation. I'm not sure if the following scenarios will be helpful or just throw you into a wild goose chase, but here we go:
/broken/one
, DO NOT REFRESH/foo
.We also created a few other examples, under the routes
/working/*
. The only difference here is that we either load 2, 3, or 0 of the buttons. For some reason, these seems to work fine, but if you load only one button, as shown inbroken/one
andbroken/two
, the styling will be broken.If you don't lazy load, everything is fine
Back to something that makes sense. The example
/working/three
shows that the blue button will always be the correct color. It is not dynamically imported in /foo, and thus does not have the same bug that the dynamically imported buttons do.What's going on in the DOM?
On /broken/one, we see the button has the class
comp-one_button__MtA0_
which is in the stylesheet0ff09bacf4af1553.css
.When we click into
/foo
, the button still has the same class, but the stylesheet is not being applied.I'm not sure what the root cause of this might be, but there's somehow a stylesheet missing when dynamically importing, and it seems to be related to using a component on the previous component inside the dynamically imported component.
The text was updated successfully, but these errors were encountered: