-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 + next.js + 'use client' #4025
Comments
Only the scope within the file where 'use client' was used will be rendered on the client-side. Example: If the index.js component contains 'use client' at the top, it will be rendered on the client-side. If the index.js component contains another component within it, that component will also be rendered on the client-side. Now, if you use the index.js component (on the client-side) within a server-side component, the index.js component will remain on the client-side, and the server-side component will stay on the server-side. It's frustrating to have to keep declaring 'use client'. The thing is, we're waiting for someone to innovate with a CSS-in-JS solution that handles SSR. I believe some libraries will die because they don't know how to deal with this.
|
I understand that next.js is a very cool technology, which is famous not only for server rendering. I love styled components very much, but the problem is that I have to prescribe 'use client' everywhere and prescribe it - this is not the main problem. The problem arises in the fact that I have practically no server components at all, because SC is used for each and everywhere you need to prescribe 'use client' and because of this the main meaning is lost next.js . It's just that optimization is important to me, and such a breakthrough technology as server rendering helps to optimize your application very well, and when you have to turn all components into client components everywhere, then the meaning is lost. I really hope that you will come up with something with the developers next.js |
I also used styled-components, but when server components have come it became not so smooth in terms of DX. I had a UI library and everywhere where I create styled component I had to put 'use client' (or just to index.js file). However, you do not need to put 'use client' in server components where you just consume the component from UI kit. So my approach was just to extract all styled components into separate files, mark them as 'use client' and then I can freely use them in server components because you can insert client components into server components. But there was a problem with
@MrOxMasTer Even with 'use client' you still have server side rendering, so there is no downgrade from "pages" approach As I understand the core problem is that styled components use context, which is not supported on server side |
The problem of extracting to a separate file will not help. The components are rendered on the client side anyway and I have almost any element, the shell is SC. Server rendering is minimal because yes, SC uses a context that does not support server rendering And what do you use for zero-style libraries. Something like talewind CSS? |
It seems to me that the emotion library for styles does not require a 'use client'. Do you know anything about this? |
I do not understand it clearly. If you have styled component it still be rendered on server side (SSR). So if you will open HTML source of your page you will see html layout from
Unfortunately no, I haven't used emotion |
And what libraries do you use for the c style next.js ? |
Currently I use https://vanilla-extract.style/ |
I just can't figure out the only thing. I don't know much about SSR and how SC works with it, and that's the question. Does it make sense to use server rendering when using SC, if we prescribe 'use client' everywhere |
If you add 'use client' you still have server side rendering, even for styled components. https://nextjs.org/docs/getting-started/react-essentials#the-use-client-directive
|
that is, it turns out that both client and server components are rendered on the server. Then it turns out that the server components just continue to be rendered on the server after the page is rendered? |
Just for example, if I replace everything with tailwind CSS and in my opinion it does not require 'use client', then this will be a more optimized solution if viewed from the point of view of NOT implementing the technology (SC is written in context and other), but from the Next js. That is, do I lose something in performance if I use SC, instead of tailwind SS (if we do not take into account that SC itself is a loading technology, but take into account SSR and other things related to next.js ) |
In that terms - yes. Because in case of SSR ('use client') your component is executed twice - first on server and second one on client. Also, everything you import in client files will be added to bundle (if you import something in server files they are not included in browser bundle and hence do not send to client) |
It's a shame, now I'm looking for a replacement for SC, although I really liked the library, but I read a lot and saw how people write that it is heavy in itself |
You currently have five options, and I have tested various libraries to reach these conclusions:
|
I went with 2. Tailwind has been pretty nice, but will be keeping my eye on this issue for sure! |
SSR works fine with Styled Components, only Server Components are not supported yet, which are two completely different things |
As someone who is coming from Vue, this is extremely confusing. |
Is this fixed? Do I really have to write 'use client' on each of my 432 |
@doksara Yes. It a pain to use styled-components in Next.Js now. I'm looking into using vanilla-extract for new projects henceforth |
Could someone inform me whether using styled components to style all my pages and components will have any impact on my SEO? I'm currently working on an e-commerce app and aim to prioritize SSR whenever feasible because of SEO. |
SEO would only be effected by page load time, which styled components will have a minor impact on since it injects the CSS at run time (but negligible, really, compared to the value of your pages to users' keywords and their relevance to the site structure you share with web crawlers via sitemaps). I recommend installing Screaming Frog to help you analyse your SEO, including impact of your changes over time. The bigger problem you'll have is making SC work with SSR, a topic which is covered extensively by every comment in this thread prior to yours. As for me I can't stand Tailwind so I guess it's back to basics, CSS all the way 🚀 (until SC can somehow resolve this challenging problem) |
Hello. I don't understand. Maybe it's some kind of mistake? Why should I write 'use client' every time I use styled-components?
I may understand that next.js does not support context or elements from libraries in the "styled" example. But just explain 2 things to me.
The text was updated successfully, but these errors were encountered: