GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(rsc): reuse server css resources element across renders by james-elicx · Pull Request #1534 · vitejs/vite-plugin-react

Conversation

@james-elicx

@james-elicx james-elicx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

Description

rscCssTransform wraps every exported server component in a CSS-importing module so that each render emits import.meta.viteRsc.loadCss(). The generated Resources component builds new <link> elements on every call, so React Flight serializes the full set of links again for every instance. React dedupes the stylesheets in the DOM, but the RSC payload (and the inlined payload in the SSR HTML) grows linearly with the number of instances.

We hit this in a real app: about 91 kB of a page's payload was the same 7 stylesheet links, repeated 398 times, once per instance of a server component that imports CSS.

deps is static for each virtual:vite-rsc/css? module, so this PR creates the element once and returns the same object from Resources. Flight already serializes an element it has written before as a short back-reference (e.g. "$0:props:children:0:props:children:0"), so every instance after the first costs a few bytes instead of the full link list.

Every instance still carries its links (as a reference), so stylesheet hoisting, precedence ordering and Suspense behaviour are unchanged. Flight tracks written objects per request, so sharing the element across requests is safe. In dev the virtual module is regenerated when its CSS changes, so the cached element can't go stale.

For scale, a standalone Flight render of 398 instances with 7 links each:

payload
before 594 kB
after 50 kB
without any links 19 kB

The new e2e test renders a CSS-importing server component N times (?test-style-server-repeated=N) and checks that the number of data-rsc-css-href entries in the RSC payload is the same for N=1 and N=5, and that every instance is styled after hydration. It fails on main in both dev and build (20 vs 24 and 9 vs 13) and passes with this change.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
james-elicx added 2 commits October 28, 2026 15:23
@james-elicx
fix(rsc): reuse server css resources element across renders
62865e4
@james-elicx
chore: retrigger ci
215e0fc
@james-elicx
james-elicx marked this pull request as ready for review October 28, 2026 17:54
@james-elicx
Merge branch 'main' into rsc-memoize-css-resources
bdd6648
hi-ogawa
hi-ogawa approved these changes Nov 4, 2026
View reviewed changes

@hi-ogawa hi-ogawa left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Cool idea! Thank you!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@hi-ogawa @claude
test(rsc): explain the css link count in repeated server css test
df5099e
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@hi-ogawa
hi-ogawa merged commit 9678b48 into vitejs:main Nov 4, 2026
21 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@james-elicx
james-elicx deleted the rsc-memoize-css-resources branch November 4, 2026 11:36
@vite-release-bot vite-release-bot Bot mentioned this pull request Nov 5, 2026
release: [email protected] #1552
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@hi-ogawa hi-ogawa hi-ogawa approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@james-elicx @hi-ogawa
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.