NEWS

5 min read

GitHub completes migration from CSS-in-JS to CSS Modules, reports 55% faster SSR

GitHub completes migration from CSS-in-JS to CSS Modules, reports 55% faster SSR
Image: Redação iMasters
News
Reading
5 min
Code
1
In short

After three years migrating 7,760 uses of the sx prop to CSS Modules, GitHub's Primer design system reports 55% faster server-side rendering and 25% faster component initialization.

GitHub published a detailed account of a migration that took three years: taking github.com off CSS-in-JS (styled-components) and moving it entirely to CSS Modules. The result, according to engineers on Primer, the company's internal design system, was a 55% reduction in server-side rendering (SSR) time and a 25% improvement in component initialization on pages that use the system. The piece, written by Josh Black and Marie Lucca, details the original problem, the rollout plan, and the role AI agents played in the final stretch.

The problem started showing up in 2023

According to the account, certain github.com pages began to see an explosion in component count starting in 2023. With styled-components, every new component generated <style> tags at runtime, both on initial load and on every style update. The more components, the more expensive it got: load time, server-side rendering time, and style-update cost all grew together.

The team chose CSS Modules as the destination because the approach preserves native CSS features and style co-location with the component, while eliminating runtime work on both client and server. Styles become static stylesheets, delivered alongside the HTML, instead of being assembled in JavaScript on every request.

How the migration was carried out in practice

The process followed a pattern repeated for each Primer component: create a CSS Modules variant, put it behind a feature flag, validate visual parity with regression tests, and only then roll it out in stages, from the Primer team to GitHub staff and, finally, to all users. By December 2024, Primer itself was already entirely on CSS Modules.

The hardest part came next: around 7,760 uses of the sx prop scattered across github.com, which for years had been the de facto standard for styling in Primer. A transitional package, @primer/styled-react, kept sx working against already-migrated components throughout the crossing, avoiding a single break-everything-at-once moment.

A typical conversion example shown in the report:

jsx
// Before
import { Button } from '@primer/styled-react';
<Button sx={{ bg: 'accent.emphasis', px: 3 }}>Pagar</Button>

// After
import { Button } from '@primer/react';
import styles from './PayButton.module.css';
<Button className={styles.pay}>Pagar</Button>

The heavy lifting, and where the agents came in

A rotating group of eight engineers converted 6,419 sx props over six months, with SSR gains ranging from 3% to 22% per page, depending on each screen's component density. The last 895 props, however, were resolved in three weeks by just two engineers leaning heavily on Copilot code agents.

The difference in pace between the two phases stands out: the manual bulk of the work took half a year with eight people, and the remainder, harder precisely because it was what was left over (the less standardized cases), was cleared in weeks with a fraction of the team. A VS Code plugin and an internal codemod had already cut down part of the repetitive work even before the agents came in.

Before removing styled-components for good, GitHub also had to decouple its theming system, which covers seven themes and high-contrast variants, so that theme switching would keep working without the CSS-in-JS runtime. The fact that styled-components had entered maintenance mode reinforced the decision not to delay the migration.

The technical community's reaction

GitHub's account reverberated on Hacker News, with experiences pulling in different directions. One developer who had carried out a similar migration reported a 30% improvement in build time and much better tree shaking on a Next.js codebase with more than 1 million lines.

Other comments were more skeptical about the gain perceived by the end user. A recurring argument is that the size reduction wouldn't be worth making all CSS class names unreadable, since hashed class names make life harder for ad blockers, custom CSS, and browser extensions that depend on stable selectors. Another discussion in the same thread pointed out that it's possible to zero out the CSS-in-JS runtime without migrating to CSS Modules, by using libraries that already compile styles at build time.

Part of a bigger trend

GitHub isn't alone in this move away from runtime CSS-in-JS. Linear recently completed its own migration, spanning around 1,000 pull requests, swapping styled-components for StyleX, Meta's atomic CSS library, citing similar concerns about rendering time after adopting React 18's concurrent mode. Other teams have solved the same problem with vanilla-extract or Tailwind, moving style generation entirely to build time.

The common thread across these approaches is paying the cost of generating CSS once, at build time, instead of recalculating it on every server request or every client render. It's this choice of where to pay the cost, not the syntax itself, that's at stake when a team decides to move away from CSS-in-JS.

What changes for those maintaining a design system

For teams working with a large component base, especially with SSR in React, GitHub's case is concrete evidence that runtime CSS-in-JS has a scaling ceiling: the cost grows along with the number of components per page, it doesn't stay constant. Teams already feeling this symptom (slow SSR, heavy hydration, expensive style updates) now have a public reference point and an incremental migration path, component by component, behind a feature flag.

The account is also practical evidence about using code agents for mechanical migration: in the final 895 cases, two engineers with Copilot did in three weeks what, at the earlier pace, would have taken proportionally more time with more people. That doesn't eliminate the need for visual regression testing and human review (GitHub kept both at every stage), but it does suggest where agent use pays off most: repetitive, well-specified conversion tasks, not the design of the migration architecture itself.

Anyone wanting to follow the same path can start with the Primer React v38 release notes and Primer's own CSS variable migration guide, which covers specifically the switch from the sx prop to CSS Modules.

Translated from the Brazilian Portuguese original · Read the original

Read also