-
Notifications
You must be signed in to change notification settings - Fork 169
fix: defer virtual rendering until after first commit for SSR safety #360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import React from 'react'; | ||
| import { renderToString } from 'react-dom/server'; | ||
| import List from '../src'; | ||
|
|
||
| const ITEM_HEIGHT = 20; | ||
|
|
||
| function genData(count: number) { | ||
| return new Array(count).fill(null).map((_, index) => ({ id: String(index) })); | ||
| } | ||
|
|
||
| describe('List.ssr', () => { | ||
| function ssr(extra?: Record<string, any>) { | ||
| return renderToString( | ||
| <List<{ id: string }> | ||
| data={genData(100)} | ||
| height={100} | ||
| itemHeight={ITEM_HEIGHT} | ||
| itemKey="id" | ||
| {...extra} | ||
| > | ||
| {(item) => <div key={item.id}>{item.id}</div>} | ||
| </List>, | ||
| ); | ||
| } | ||
|
|
||
| it('initial SSR render should not enter virtual mode (no scrollbar / Filler wrap)', () => { | ||
| const html = ssr(); | ||
| expect(html).not.toContain('rc-virtual-list-scrollbar'); | ||
| // No Filler outer wrap with translateY (only emitted when inVirtual is true) | ||
| expect(html).not.toContain('translateY'); | ||
| }); | ||
|
|
||
| it('initial SSR render should use native overflow:auto on holder', () => { | ||
| const html = ssr(); | ||
| // overflow-y stays "auto" before mount measures the container | ||
| expect(html).toContain('overflow-y:auto'); | ||
| expect(html).not.toContain('overflow-y:hidden'); | ||
| }); | ||
|
|
||
| it('initial SSR render should not render horizontal scrollbar even when scrollWidth is set', () => { | ||
| const html = ssr({ scrollWidth: 200 }); | ||
| expect(html).not.toContain('rc-virtual-list-scrollbar'); | ||
| expect(html).not.toContain('overflow-x:hidden'); | ||
| }); | ||
|
|
||
| it('initial SSR render emits all items so the page is readable without JS', () => { | ||
| const html = ssr(); | ||
| // First and last item should both appear in SSR output | ||
| expect(html).toContain('>0</div>'); | ||
| expect(html).toContain('>99</div>'); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
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.
🚨 Performance Hazard: Rendering All Items on SSR / First Client Render
By gating
inVirtualonmounted, the list will render in non-virtual mode during SSR and the first client render (hydration). This means all items in thedataarray will be rendered to the DOM (sincestartis0andendisdata.length - 1wheninVirtualis false).For large datasets (e.g., 1,000+ items), this completely defeats the purpose of virtualization on initial load, leading to:
mountedbecomestrue.💡 Solution: Render a Safe Subset on SSR & First Render
We can achieve both SSR safety (no hydration mismatch) and high performance by rendering a limited subset of items (e.g., based on
height / itemHeight) whenmountedisfalseanduseVirtualistrue. Since both SSR and the first client render will render the exact same subset, hydration will succeed perfectly without mismatch, and we avoid rendering the entire dataset.1. Update the
start/endcalculation (around line 229):2. Prevent redundant
onVisibleChangecalls on initial mount (around line 563):Currently,
onVisibleChangewill be called with the entire dataset on the first render, and then immediately called again with the virtualized subset after mount. We can gate this effect so it only triggers when virtualization is ready: