Meta

How Meta rebuilt facebook.com for speed

Atomic CSS that shrank homepage CSS by 80%, JavaScript loaded in tiers, code chosen by the data it renders, and data fetched in parallel with code: the architecture behind the 2020 facebook.com redesign.

Based on Rebuilding our tech stack for the new Facebook.com by Ashley Watkins and Royi Hagigi (Meta, 8 May 2020).

10 min read · Bloom is not affiliated with Meta. Read the original for the full story.

TL;DR

  • Meta rebuilt facebook.com from a server-rendered PHP app with layered enhancements into a client-side React app with GraphQL (Relay).
  • Two principles drove everything: deliver as little as possible, as early as possible, and build tooling so engineers get performance and accessibility by default.
  • Atomic CSS (StyleX) cut homepage CSS from about 400 KB to under 20 KB compressed, and CSS variables made dark mode a theme switch, not a second stylesheet.
  • JavaScript loads in three tiers, rendering code is chosen by the data returned, and data is fetched in parallel with code instead of after it.

The problem

By 2020, facebook.com was a server-rendered PHP application with years of client-side enhancements layered on top. The redesign had product goals that the old stack made hard: an app-like, interactive experience, features like dark mode, and keeping your place in News Feed, all while being fast to start up.

Rather than patching the old stack, the team rebuilt it around two principles:

  1. "As little as possible, as early as possible." Ship only the resources a screen needs, exactly when they're needed.
  2. Engineering experience in service of user experience. Design the APIs and tooling so the fast, accessible path is the default path for thousands of engineers.

The second principle is the architecturally interesting one: instead of relying on every engineer to remember performance rules, they encoded the rules in the framework.

CSS: atomic styles and theming with variables

The old site had grown a large CSS payload, and only a small fraction of it (the article says about 10%) was used to render the homepage initially.

The fix was atomic CSS generated from CSS-in-JS (Meta's StyleX). Engineers write styles colocated with their React components; at build time, each unique declaration (like color: blue) becomes one small reusable class. Because the stylesheet grows with the number of unique declarations rather than the number of features, growth is roughly logarithmic. The result: homepage CSS went from about 400 KB to under 20 KB compressed, an 80% reduction.

Dark mode used to mean extra class overrides fighting specificity. The rebuild switched to CSS variables scoped under theme classes: one atomic stylesheet serves every theme, switching themes needs no reload, and different parts of the product can use different themes without downloading more CSS.

A small accessibility detail worth copying: engineers write font sizes in pixels, and the build converts them to rem, so text respects the user's browser font-size setting.

JavaScript: tiers, data-driven code, and budgets

Three loading tiers. Code is split by when it's needed, using declarative, statically analyzable imports instead of ad-hoc dynamic imports during render:

  • Tier 1: just enough for the above-the-fold layout and loading skeletons
  • Tier 2: everything needed to fully render and interact with the above-the-fold content
  • Tier 3: things that aren't needed to display the page, like logging and subscriptions

The article illustrates this with an example page whose 500 KB of JavaScript splits into tiers of 50 KB, 150 KB, and 300 KB. Those are illustrative numbers, not measurements.

Data-driven dependencies. A feed can contain photo posts, video posts, events, and more. Downloading the rendering code for every type up front is wasteful. With Relay's @module directive, the query result decides which component code is fetched: a photo story brings PhotoComponent, a video story brings VideoComponent.

Experiment-driven dependencies. Conditional imports let the server send only the A/B test variant a user is actually in.

JavaScript budgets. Each product area has a size budget, enforced with dependency graph tooling, size checks on pull requests, and dashboards that track size over time per product and team.

Data: fetch in parallel with code, then stream

The classic single-page-app waterfall is: download JavaScript, run it, then request data. Meta removed that wait by preloading the GraphQL query on the initial server request, so data starts flowing while code is still downloading, and streams to the client as it becomes available.

Two GraphQL directives keep slow parts from blocking fast ones:

  • @stream sends feed stories one by one as they're ready, instead of waiting for the whole list.
  • @defer sends slower sections later. For example, a profile's name and photo render immediately while timeline data arrives afterwards, with React Suspense showing loading states in between.

Navigation: route maps and EntryPoints

For fast navigation, the router keeps a route map of which code and data each route needs, filled in as links appear on screen. Resources are prefetched on hover or focus, and fetching starts on mousedown, before the click completes.

EntryPoints are small wrappers around code-split points that turn route parameters into a GraphQL query. Because they're downloaded ahead of time, the app can fetch a route's code and data in parallel, in one round trip, instead of fetching code and then discovering what data it needs. React Suspense transitions keep the previous page visible until the next one has a good loading state, which avoids blank screens.

  Classic SPA (waterfall)                     Rebuilt facebook.com
  ───────────────────────                     ───────────────────────────────────────────
  HTML ─▶ JS bundle ─▶ run ─▶ fetch data      HTML ─┬─▶ JS tier 1 (skeleton) ─▶ tier 2 ─▶ tier 3
                         └─▶ render                 └─▶ GraphQL query preloaded by the server
                                                        ├─ @stream: feed stories one by one
                                                        └─ @defer: slow sections later
                                                    data decides component code (@module)
Initial page load: code and data in parallel (simplified)

Key decisions and trade-offs

DecisionWhyTrade-off
Atomic CSS generated from colocated CSS-in-JSStylesheet size grows with unique declarations, not features; unused CSS disappears.Requires a build-time compiler and a styling API teams must adopt.
Tiered, statically analyzable code splittingThe build can plan exactly what loads when, and the page displays before non-visual code arrives.Custom import APIs and tooling instead of plain dynamic import().
Code chosen by query results (@module)Only the renderers for the content actually shown are downloaded.Ties code loading to the GraphQL/Relay stack.
Preload data on the initial request and stream itRemoves the code-then-data waterfall and lets fast content render first.Server, router, and data layer must cooperate closely (EntryPoints, route maps).

What this means for your frontend

  • Measure how much of your CSS the first screen actually uses. If it's a small fraction, atomic or utility-first CSS (for example Tailwind, which works on the same principle) can shrink it dramatically.
  • Implement themes and dark mode with CSS variables, so switching themes is free and needs no extra stylesheet.
  • Split JavaScript by when it's needed (skeleton, interactive, after display), not only by route.
  • Kill the code-then-data waterfall: start data requests on the server or in parallel with code (React Server Components, route loaders, or prefetching queries).
  • Turn performance rules into tooling: per-team bundle budgets with PR checks beat performance guidelines in a wiki.

Check your understanding

Why did atomic CSS shrink facebook.com's stylesheet so much?

What problem does preloading the GraphQL query on the initial server request solve?

Go deeper on Bloom

Explainer written 22 October 2026.