Skip to content

Experimental RSC Support #5624

Closed
Closed

Description

@quantizor

Hi folks, I have a PR up #5620 that adds a flavor of RSC support to the library by selectively disabling certain incompatible features (like React Context). To test it, install styled-components@prerelease and try removing the 'use client' from the files you're using s-c in.

Please 👍 this post if it works for your setup, if it doesn't then any debugging info you can provide would be very helpful. I just don't have a good complex production test involving RSC + styled-components in my professional sphere at the moment.

I'll leave this up for a while, if it seems like we get a critical mass of 👍 then I'll release it.

Activity

  1. pinned this issue on Jan 6, 2026
  2. Jacksonmills commented on Jan 6, 2026

    @Jacksonmills

    Since I don't have any active projects using styled-components at the moment, I opted to test the integration using a fresh create-next-app base. Using Next.js 16.1.1 and Node v22.20.0, I converted the default homepage from CSS Modules to styled-components@prerelease. The entire page rendered server-side perfectly with no FOUC or hydration mismatches. 🚀

  3. quantizor commented on Jan 6, 2026

    @quantizor
    ContributorAuthor

    @Jacksonmills so good to see you around, thank you for testing it 😁

  4. soujvnunes commented on Jan 9, 2026

    @soujvnunes

    I've tested styled-components@prerelease with Next.js and can confirm it works!

    I removed 'use client' from component files using styled-components and the server component rendering them have no FOUC or hydration issues:

    // shared/components/text.ts (NO 'use client' needed!)
    export const Typography = styled.h1`
      &[data-variant~="body"] { font-size: 16px; }
      &[data-variant~="title"] { font-size: 24px; }
    `
    
    // app/page.tsx (Server Component)
    import { cookies } from 'next/headers'
    export default async function Page() {
      const isAuth = (await cookies()).get('token')?.value
      return (
        <Typography data-variant={isAuth ? 'title' : 'body'}>
          {isAuth ? 'Welcome back!' : 'Please connect.'}
        </Typography>
      )
    }

    Just some notes I'd document:

    1. I had to prefer styling ahead-of-time to avoid dynamic styles due to server-side serialisation.
    2. For those wanting to avoid hardcoded values while maintaining RSC compatibility, I've found that build-time CSS variable generation (like themizer) works well since it creates static CSS files that work in both Server and Client Components, which would remove the need to implement ThemeProvider (smaller bundle)
  5. quantizor commented on Jan 10, 2026

    @quantizor
    ContributorAuthor

    @soujvnunes thank you, great callout. I will add content for this to the release notes and FAQ!

  6. quantizor commented on Jan 10, 2026

    @quantizor
    ContributorAuthor

    Shipping this tonight, thank you testers!

  7. soujvnunes commented on Jan 10, 2026

    @soujvnunes

    @quantizor buddy, just caught it now. I noticed this warning on the server

    The component styled.html with the id of "sc-bdfBQy" has been created dynamically.
    You may see this warning because you've called styled inside another component.
    To resolve this only create new StyledComponents outside of any render method and function component.
    See https://styled-components.com/docs/basics#define-styled-components-outside-of-the-render-method for more info.
    
        "next": "^16.1.1",
        "react": "^19.2.3",
        "react-dom": "^19.2.3",
        "styled-components": "^6.3.1",
    
  8. quantizor commented on Jan 10, 2026

    @quantizor
    ContributorAuthor

    @soujvnunes any chance you could share code samples around the component giving you issues?

  9. soujvnunes commented on Jan 10, 2026

    @soujvnunes

    @soujvnunes any chance you could share code samples around the component giving you issues?

    Sure

    https://codesandbox.io/p/devbox/qk5y5d

    Image
  10. quantizor commented on Jan 10, 2026

    @quantizor
    ContributorAuthor

    Cool, thanks! I think it's a false positive. Will fix.

  11. unpinned this issue on Jan 11, 2026
  12. laurenskling commented on Jan 12, 2026

    @laurenskling

    Thank you for your work on this!
    So we are supposed to not have a theme right? When we drop use client. Isn't that confusing? I didn't notice at first, my values where just undefined. It doesn't break, because there is an empty theme object in the props. Maybe it's even better to not have the theme object, so my code will crash, giving me an indicator I have to fix this file.

  13. laurenskling commented on Jan 12, 2026

    @laurenskling

    I've hit a use case:

    'server-only';
    import CoreButton from '@core/button'; // is a 'use client'
    
    export const Button = styled(CoreButton)`
      background-color: black;
    `;
    

    this doesn't work properly anymore. Ends up, the specificity lost. This re-styling used to win in specificity (as it was later in the ordering?) but now it's lower. I guess due to from the server injecting losing by order from client rendering of the styles, which come later down the file?

  14. laurenskling commented on Jan 12, 2026

    @laurenskling

    I have this:

    'server-only';
    import styled, { createGlobalStyle } from 'styled-components';
    
    export const BodyLockStyles = createGlobalStyle`
      body {
        overflow: hidden !important;
      }
    `;
    

    I use it for my nav:

    'server-only';
    
    const CoverNav = () => {
      return (
        <RenderOnOpen renderOnOpenIs={true}>
          <BodyLockStyles />
          <nav/>
        </RenderOnOpen>
      );
    };
    

    using this trick wrapper I created to read context:

    'use client';
    
    import { useNav } from '../nav-context';
    
    const RenderOnOpen = ({ renderOnOpenIs = false, children }) => {
      const { open } = useNav();
      if (open !== renderOnOpenIs) {
        return null;
      }
      else {
        return children;
      }
    }
    
    export default RenderOnOpen;
    

    This all starts off working fine, the global styles are added when I open the nav, the context goes to true, the BodyLockStyles is rendered. head now contains: <style data-styled-global="sc-global-krxNsn" data-href="sc-global-sc-global-krxNsn-1-dajaO" data-precedence="styled-components">body{overflow:hidden!important;}/*!sc*/ </style>
    Problem is, when I close the nav, this style tag isn't removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions