Skip to content

[docs-infra] Decrease loaded bundle size on docs#48584

Merged
brijeshb42 merged 5 commits into
mui:masterfrom
brijeshb42:bundle-size
May 28, 2026
Merged

[docs-infra] Decrease loaded bundle size on docs#48584
brijeshb42 merged 5 commits into
mui:masterfrom
brijeshb42:bundle-size

Conversation

@brijeshb42
Copy link
Copy Markdown
Contributor

@brijeshb42 brijeshb42 commented May 28, 2026

  1. Pin stylis and react-is to single version (stylis at 4.2.0 since that's what emotion depends on and react-is at latest 19.2.6), otherwise 2 different versions of both were part of the bundle.
  2. Update AppSearch to use React Portal instead of ReactDOMServer.rendertoStaticMarkup. This removes react-dom/server from the client bundle.
  3. Load styled-components and @mui/stylis-plugin-rtl conditionally, when user enables the rtl option from sidebar. This reduces the initial load size by 12kB gzip (33 kB raw).

Total savings measured at ~ 69 KB over the wire and ~ 200kB decoded.

Some page wise data
Page JS gzip Δ JS decoded Δ
/ (home) −52.6 KB −169.4 KB
/material-ui/getting-started/installation/ −62.1 −191.3
/material-ui/react-button/ −69.3 −219.1
/material-ui/react-text-field/ −69.3 −219.1
/material-ui/react-autocomplete/ −69.6 −219.1
/material-ui/react-table/ −69.6 −219.1
/material-ui/react-dialog/ −69.3 −219.1
/system/getting-started/custom-components/ −56.7 −187.1

@brijeshb42 brijeshb42 requested a review from a team May 28, 2026 07:05
@brijeshb42 brijeshb42 added the scope: docs-infra Involves the docs-infra product (https://www.notion.so/mui-org/b9f676062eb94747b6768209f7751305). label May 28, 2026
@code-infra-dashboard
Copy link
Copy Markdown

code-infra-dashboard Bot commented May 28, 2026

Deploy preview

https://deploy-preview-48584--material-ui.netlify.app/

Bundle size

Bundle Parsed size Gzip size
@mui/material 0B(0.00%) 0B(0.00%)
@mui/lab 0B(0.00%) 0B(0.00%)
@mui/private-theming 0B(0.00%) 0B(0.00%)
@mui/system 0B(0.00%) 0B(0.00%)
@mui/utils 0B(0.00%) 0B(0.00%)

Details of bundle changes


Check out the code infra dashboard for more information about this PR.

Comment thread package.json Outdated
},
"pnpm": {
"overrides": {
"stylis": "4.2.0",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should never do overrides for packages that are published

Suggested change
"stylis": "4.2.0",
"docs>stylis": "4.2.0",

@Janpot
Copy link
Copy Markdown
Member

Janpot commented May 28, 2026

Maybe make the overrides in pnpm-workspace.yaml so that we can add comments?

1. Pin stylis and react-is to single version, otherwise 2 different
versions of both were part of the bundle.
2. Update AppSearch to use React Portal instead of
`ReactDOMServer.rendertoStaticMarkup` since portal is already part of
the bundle.

Total savings measured at −57.7 KB wire / −409 KB decoded.
<div className="DocSearch-NewStartScreenTitle">{category.name}</div>
{items.map(({ name, icon, href }) => (
<NextLink key={name} href={href} className="DocSearch-NewStartScreenItem">
<a key={name} href={href} className="DocSearch-NewStartScreenItem">
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise Next.js tries to prefetch the pages which is new behavior.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that a problem?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a problem. Earlier plain html got rendered even with NextLink, so the links were not detected by next.js. But with portal, these are proper Next.js Link.
It was hard to measure the bundle diff with these prefetch in places. I'll revert at the end so prefetching works.

Move StyleSheetManager (styled-components) and rtlPlugin
(@mui/stylis-plugin-rtl)
into a shared intermediate module `utils/rtlBundle.ts`, then
dynamic-import it
from both StyledEngineProvider and DemoSandbox only when direction ===
'rtl'.

Verified via the next.js build-manifest (497 pages total):

1. Only 2 / 497 pages statically pull the styled-components lib chunk
(35990):
   /performance/table-styled-components and
/system/getting-started/custom-components
   — i.e., the two pages that import `styled-components` directly. No
leak into
   other pages' bundles.
2. 0 / 497 pages statically pull the rtl bundle chunk (26461). It is
fetched
   only on RTL activation, via dynamic import.
3. Pages whose markdown mentions `styled-components` in code examples
   (interoperability, v5-style-changes, right-to-left) contain the text
but do
   not import the library — they are NOT in the manifest's deps for
35990.

Per-page bundle diff vs deploy-preview-48584 (raw decoded / gzip):

| chunk          | local raw | deploy raw | Δ raw      | Δ gzip     |
|----------------|-----------|------------|------------|------------|
| _app.js        | 652.6 KB  | 680.0 KB   | -27.4 KB   | -10.6 KB   |
| 22072.js (Demo)| 48.3 KB   | 49.6 KB    | -1.25 KB   |  ~0        |
| net per page   |           |            | -28.5 KB   | -10.6 KB   |

Savings come from removing styled-components' StyleSheetManager and the
rtl
stylis plugin from the always-loaded _app + Demo chunks.

RTL toggle cost on a non-sc page: 26461 (~5 KB raw / ~2 KB gzip) +
35990 (~33 KB raw / ~12 KB gzip), fetched once per session.

The only page with a small regression is
/performance/table-styled-components
(+2 KB gzip) because the styled-components lib now lives in a separate
chunk
(35990) instead of being inlined into the page chunk.
@brijeshb42 brijeshb42 requested a review from Janpot May 28, 2026 08:57
Copy link
Copy Markdown
Member

@Janpot Janpot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, do we need the same on X?

@brijeshb42
Copy link
Copy Markdown
Contributor Author

Yes. But that'll propagate automatically in next version bump of core-docs which is blocked on latest release of material-ui.

@Janpot
Copy link
Copy Markdown
Member

Janpot commented May 28, 2026

Yes. But that'll propagate automatically

But the override won't propagate, right?

@brijeshb42
Copy link
Copy Markdown
Contributor Author

Yes. That'll need manual change. I'll use this PR as a reference anyways.

@brijeshb42 brijeshb42 merged commit f553f26 into mui:master May 28, 2026
18 checks passed
@brijeshb42 brijeshb42 deleted the bundle-size branch May 28, 2026 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: docs-infra Involves the docs-infra product (https://www.notion.so/mui-org/b9f676062eb94747b6768209f7751305).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants