next.js
next.js copied to clipboard
Next 13 CSS modules support doesn't load stylesheets from client components
Verify canary release
- [X] I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: linux
Arch: x64
Version: Ubuntu 20.04.0 LTS Fri Oct 28 2022 16:34:05 GMT+1000 (Australian Eastern Standard Time)
Binaries:
Node: 16.14.2
npm: 7.17.0
Yarn: 1.22.19
pnpm: 7.13.6
Relevant packages:
next: 13.0.1-canary.0
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
What browser are you using? (if relevant)
Firefox Developer Edition 106.0b8
How are you deploying your application? (if relevant)
next dev
Describe the Bug
A client component is created with the "use client"
directive in the app
directory. This client component uses a .module.css
file to (attempt) to color the contents of a span
red. Although the class name is generated and applied to the element, the corresponding stylesheet is never loaded. If the "use client"
directive is removed, everything works correctly.
Expected Behavior
The stylesheet should be loaded the the contents of the span
should be displayed in red.
Link to reproduction
https://stackblitz.com/edit/vercel-next-js-wfksse?file=app/Client.jsx
To Reproduce
- Create a new next project.
- Delete
pages
. - Enable the
app
directory in the config. - Use a standard
layout.jsx
:
export default function RootLayout({
children,
}) {
return (
<html lang="en">
<head></head>
<body>
{children}
</body>
</html>
);
}
- Use this
page.jsx
:
import Bar from "./Bar";
export default function Page() {
return <Bar />;
}
- Create a file named
app/Bar.jsx
with this content:
"use client";
import styles from "./Bar.module.css";
export default function Bar() {
return <span className={styles.bar}>Hello, World!</span>
}
- Finally, create a file named
app/Bar.module.css
with this content:
.bar {
color: red;
}
Now run npm dev
or npm build && npm start
to observe the bug.
Workaround: importing the stylesheet from a different file that isn't a client component, such as layout.jsx
, seems to fix the issue.
Duplicate #41791
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.