Next js 14 styled Components Loading Issue at first time ,after refresh resolved . #70581
Replies: 2 comments 13 replies
-
CSS-in-JS libraries which require runtime JavaScript are not currently supported in Server Components. Please, Check this doc: |
Beta Was this translation helpful? Give feedback.
-
Saying that this is not supported, is not 100% correct. What happens is that you need to do a bit more work to get the Styled Components working. You need to follow this guide: https://nextjs.org/docs/app/building-your-application/styling/css-in-js#styled-components The useServerInsertedHTML(() => {
const styles = styledComponentsStyleSheet.getStyleElement()
styledComponentsStyleSheet.instance.clearTag()
return <>{styles}</>
}) This makes it so that the styles are collected during SSR, and injected into the document, so that your components are styled properly, before JS is run.
You just need to copy paste the code in the link (https://nextjs.org/docs/app/building-your-application/styling/css-in-js#styled-components), and you are good to go. |
Beta Was this translation helpful? Give feedback.
-
Summary
My package Setup is
Next.js version: 14.1.4
Styled-components version: 6.1.8
I'm seeking your assistance with an issue I've encountered in my production environment. Specifically, styled-components are not loading correctly on the initial page load, despite working fine in development mode. and after refresh page it works perfectly.
Additional information
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions