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
styled-components are not working with Next.js SSR #3634
Comments
Did you update your Check out this example of an _document.js. |
it's quite compulsory to declare the above code in |
@dhavalveera It's required. Just add it to your |
@dhavalveera I believe it should be:
(notice it's just |
if I put the above-shared code in |
@dhavalveera You can put them in the same file. Here's an example: https://github.com/agriffis/agcom/blob/4783dcb9eeae67de8765a551f4336e7068eed335/pages/_document.tsx Be aware that this is probably not the source of the problem you're experiencing. Rendering styles in SSR is an optimization, not a requirement. When you say:
That means that you have some other issues in your code that are probably not going to be solved by enabling SSR rendering of styles. |
Duplicate of #3634 @dhavalveera Please don't open duplicates across repos |
For newcomers to this issue, or if your app is running the new SWC compiler(default from v12), simply add the following to next.config.js:
https://nextjs.org/docs/advanced-features/compiler#styled-components |
for some reason even tho I added it still having same issue.. :( |
solved? im having the same issue |
solved for me! |
// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
compiler: {
reactStrictMode: true,
styledComponents: true,
},
};
module.exports = nextConfig; Add |
Actually, I've looked into this issue and my conclusion is that there is not really a way to make styled-components to be rendered on the server side, at least not with Next.js 13 and app router... But it is possible to make styled-components work with Next.js 13 without any "delay bugs" using client-side rendering and a few workarounds. Step 1: add the following to your next.config.js file
Step 2: create the registry.tsx file with the following code:
Step 3: add the 'use client' directive to your layout.tsx file and wrap all the children components on your layout with the StyledComponentsRegistry component.I've made a tutorial if anyone needs further help: |
Thank you! You saved me from mental breakdown :)
Thank you! You saved me from a mental breakdown :) |
Hello,
I've recently come to know about the
babel-plugin-styled-components
and added the following code snippet into.babelrc
.but when I explicitly refresh my browser, the styled components are not working as defined. like styled-components don't get loaded as required.
I tried to change the
"ssr": true
as well but not working.please anyone can help me to solve the issue I am facing?
The text was updated successfully, but these errors were encountered: