Skip to content

perf(suspense): optimize handling of multiple suspense/useSubscribe #441

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

Merged
merged 2 commits into from
Apr 24, 2025

Conversation

welkinwong
Copy link
Contributor

Strictly speaking, this is indeed a bug. While it doesn’t break the app, it can lead to a poor user experience in specific scenarios:

When two or more identical subscriptions are active simultaneously, unmounting one of them may cause the other subscription’s nearest Suspense boundary to revert to its fallback state.

This issue typically occurs in complex applications.


This PR primarily introduces a reference counter. The subscription will only be cleared when the counter reaches zero, indicating that no components are actively consuming it.

@welkinwong
Copy link
Contributor Author

before:
Snipaste_2025-04-17_20-56-50

after:
Snipaste_2025-04-17_20-55-13

@nachocodoner
Copy link
Member

nachocodoner commented Apr 22, 2025

Not sure if it's related, but it might be. In the forums, an user reported a failure in [email protected], including the recent changes we merged around Suspense, useFind, and useSubscribe. One user said the react-router-ssr-demo doesn’t load data at all when using 3.0.5-beta.0. The demo code uses Suspense and the hooks, so it could be closely tied to those changes. Not sure if it’s covered by the fix in this PR or if another fix is needed.

Could you check? I’ll test it when I get a break from the latest bundler work.

@welkinwong
Copy link
Contributor Author

@nachocodoner

After investigation, I believe this is an implementation issue in the v6 version of the react-router-ssr module. The current implementation uses React 18's renderToPipeableStream with onAllReady for completion signaling, but the stream doesn't wait for all asynchronous data to resolve.

I've already filed an issue regarding this behavior in the react-router-ssr repository: renderToPipeableStream in server.js Does Not Wait for Async Content Inside Suspense

@nachocodoner nachocodoner merged commit 059378a into meteor:master Apr 24, 2025
2 checks passed
@welkinwong welkinwong deleted the optimizing-multi-sps-sub branch April 27, 2025 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants