docs: add design document for lazy route definitions#138
Merged
Conversation
Replace the useEffect + resolveLazyChildren tree-walker approach with a Suspense-based design where <Outlet /> suspends via a PendingOutlet component while lazy children are being resolved. Key changes: - PendingOutlet component throws a promise (React Suspense pattern) - startTransition keeps old page visible during navigation - Suspense fallback shown only on initial page load - No separate resolveLazyChildren function needed - Error handling via React error boundaries - NavigationAPIAdapter needs no changes https://claude.ai/code/session_01HqWiPByktzUpdesqZbNcHp
- Replace throw promise with use(promise) in PendingOutlet - Rewrite "nested lazy subtrees" section without referencing the rejected resolveLazyChildren approach - Update all flow diagrams to reference use() instead of throw https://claude.ai/code/session_01HqWiPByktzUpdesqZbNcHp
Replace the module-level WeakMap and lazyVersion counter with a single Map stored in Router's useState. The cache is scoped to the Router instance, cleared when routes prop changes, and safe under concurrent rendering. PendingOutlet uses setState-during-render to register promises and setLazyCache to trigger re-render on resolution. https://claude.ai/code/session_01HqWiPByktzUpdesqZbNcHp
Move promise creation into PendingOutlet's own useState initializer instead of reading from Router's lazyCache during render. The setState-during-render pattern only works for the component that owns the state — PendingOutlet can't call Router's setLazyCache during render. Instead, setLazyCache is called from the .then() callback (outside render) as a normal async state update. https://claude.ai/code/session_01HqWiPByktzUpdesqZbNcHp
useState data is not retained in components that suspend before committing — React discards the in-progress fiber. Move promise creation into Router (which is already committed) using setState-during-render on its own state. PendingOutlet becomes a thin wrapper that just receives a promise prop and calls use(). https://claude.ai/code/session_01HqWiPByktzUpdesqZbNcHp
Allow the children function to return either synchronously or as a promise. matchRoutes now calls the function — sync results are resolved in-place immediately, async results cause a partial match. This handles the edge case where Router can't commit (state lost): on re-render, the user's cache returns sync and matchRoutes resolves it without any suspension. Add caching contract section explaining the user's responsibility to cache the Promise and resolved result. Also add Router suspension edge case, updated detailed behavior flows, and test cases for sync resolution. https://claude.ai/code/session_01HqWiPByktzUpdesqZbNcHp
Route objects are never mutated. matchRoutes uses the sync result as a local variable for matching; Router's .then() handler only triggers a re-render via setLazyCache (no route.children assignment). After resolution, subsequent matchRoutes calls invoke the user's function which returns the cached array synchronously. This keeps route definitions immutable — the user's caching function is the single source of truth for resolution state. https://claude.ai/code/session_01HqWiPByktzUpdesqZbNcHp
The setLazyCache call in the .then() handler is specifically needed for the initial page load case. During navigation, startTransition retries the entire transition (including Router) automatically. But on initial page load there is no transition — only the Suspense subtree retries, and Router is above the Suspense boundary. Without the .then(), PendingOutlet returns null after resolution, leaving an empty outlet. https://claude.ai/code/session_01HqWiPByktzUpdesqZbNcHp
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
https://claude.ai/code/session_01HqWiPByktzUpdesqZbNcHp