Urgent.News

What's breaking now, across hundreds of outlets.

Tech

GitHub Ditches CSS-in-JS for CSS Modules, Reports 55% Faster SSR After Three-Year Migration

GitHub migrated github.com from styled-components to CSS Modules over three years, reporting a 55% cut in server-side rendering time and 25% faster component initialisation. Primer components were converted behind feature flags, and about 7,760 sx prop usages were migrated with codemods and Copilot agents. Commenters questioned user impact and hashed class names. By Daniel Curtis

GitHub, the widely-used platform for software development, has recently announced a significant shift in its approach to styling large-scale web applications. In a detailed post, Primer engineers Josh Black and Marie Lucca outline a three-year migration from CSS-in-JS to CSS Modules, reporting a substantial 55% reduction in server-side rendering time and a 25% faster component initialisation across their Primer-powered design system.

The team's original CSS-in-JS setup, styled-components, began to struggle as the number of components on certain pages increased. This caused scaling problems with component counts, leading to longer initial page loads, increased server render times, and higher style update costs. Pricier components necessitated the exploration of an alternative solution.

CSS Modules emerged as the preferred choice due to its compatibility with native CSS features and the ability to co-locate styles with their components. By converting Primer components to CSS Modules, the team aimed to eliminate client and server runtime work, allowing styles to roll up into static stylesheets delivered alongside the HTML.

The migration process involved gradually replacing CSS-in-JS usage with CSS Modules, starting from the Primer team and eventually extending to GitHub staff and all users. A transitional package, @primer/styled-react, facilitated the smooth transition by allowing sx props to continue functioning during the crossover period. A dedicated team of eight engineers migrated 6,419 sx props across the GitHub website over six months, witnessing server-side rendering improvements ranging from 3% to 22% per page.

The final 895 props were transitioned in just three weeks by a smaller team of two engineers, leveraging an AI-powered coding agent for assistance.

The migration also required careful consideration of the Primer's seven themes and high-contrast variants. To address this, the team developed a VS Code plugin and an internal codemod to automate the process. The decision to transition was further reinforced by the fact that styled-components had entered maintenance mode.

GitHub's move aligns with a broader industry trend towards optimizing style generation and reducing runtime costs. Similar migrations have been reported by other companies, such as Linear, which successfully replaced styled-components with Meta's atomic StyleX library, citing similar render-time concerns after upgrading to React 18's concurrent mode. Other teams have opted for vanilla-extract or Tailwind, generating styles during the build process entirely.

The shift towards CSS Modules reflects a growing consensus in the development community to address the runtime costs associated with CSS-in-JS libraries. By performing the heavy lifting at build time, developers can optimize performance and create more maintainable codebases. GitHub's migration serves as a valuable case study for teams considering a similar transition, offering insights into the challenges and benefits of migrating to CSS Modules.

Rewritten by urgent.news; may contain errors — check the original.

Source: infoq.com

Read the original at infoq.com →

Report a problem with this story

More in Tech

More from Wednesday 25 November →