feat(plugin-rsc): improve client-side navigation with transitions, cache, and error handling #934
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.
Overview
This PR significantly improves the client-side navigation implementation in the RSC examples to be production-ready, addressing requirements from the original issue and issue #795.
Changes
1. Coordinated History and Transition Handling
The old implementation called
onNavigation()synchronously afterhistory.pushState(), which could cause jarring updates where the URL changes but the page hasn't rendered yet.Improvements:
React.startTransition()to coordinate with React's concurrent renderinghistory.pushState/replaceState) for instant user feedbackpendingNavigationUrltracking to prevent race conditionsReference: Inspired by https://github.com/hi-ogawa/reproductions/tree/main/vite-rsc-coordinate-history-and-transition
2. Back/Forward Cache Support
Previously, every back/forward navigation required a full re-fetch from the server, causing noticeable delays especially for content-heavy pages.
Improvements:
navigationCacheMap to store visited pagesThis is especially important for documentation sites and content-heavy applications where users frequently use browser navigation buttons.
Reference: Addresses requirements from https://github.com/hi-ogawa/rscpress
3. Navigation Cancellation
Improvements:
AbortControllerto cancel pending navigations4. Comprehensive Error Handling
Addresses issue #795 with proper error handling throughout the navigation and rendering pipeline.
Browser (
entry.browser.tsx):onErrorcallbacks to allcreateFromReadableStreamandcreateFromFetchcallsSSR (
entry.ssr.tsx):onErrorcallback torenderToReadableStreamwith error digest and component stackonErrorcallback tocreateFromReadableStreamfor RSC deserialization errorsReference: Addresses issue #795 - #795
5. Demo Navigation Links
Added navigation links to the starter example to demonstrate the improvements:
The demo shows:
Files Changed
packages/plugin-rsc/examples/starter/src/framework/entry.browser.tsx- Improved navigation logicpackages/plugin-rsc/examples/basic/src/framework/entry.browser.tsx- Improved navigation logicpackages/plugin-rsc/examples/starter/src/framework/entry.ssr.tsx- Added error handlingpackages/plugin-rsc/examples/basic/src/framework/entry.ssr.tsx- Added error handlingpackages/plugin-rsc/examples/starter/src/root.tsx- Added demo navigation linksTesting
Breaking Changes
None. These are improvements to example code only.
Related Issues
Closes #<issue_number>
Addresses #795
Credits
Thanks to @hi-ogawa for the issue report and reference implementations.
Original prompt
Fixes #860
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.