refactor(router-core): scroll-restoration minor performance cleanup#4990
refactor(router-core): scroll-restoration minor performance cleanup#4990
Conversation
WalkthroughRefactors scroll-restoration internals in a single file: adjusts error handling, refines CSS selector building, simplifies restore flow with a labeled block, improves hash parsing and state access, standardizes scroll behavior, and modernizes state initialization using ||=. No public API changes. Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant Router
participant ScrollRestoration
participant History
participant SessionStorage as SessionStorage
participant Window as Window/DOM
User->>Router: Navigate/render
Router->>ScrollRestoration: restoreScroll()
ScrollRestoration->>History: read state (__hashScrollIntoViewOptions)
ScrollRestoration->>SessionStorage: getSafeSessionStorage()
ScrollRestoration->>Window: window.scrollTo({top:0,left:0,behavior})
ScrollRestoration->>Window: For each selector: element.scrollTo({top:0,left:0,behavior})
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Suggested reviewers
Poem
✨ Finishing Touches
🧪 Generate unit tests
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
|
View your CI Pipeline Execution ↗ for commit 23fa3d8
☁️ Nx Cloud last updated this comment at |
More templates
@tanstack/arktype-adapter
@tanstack/directive-functions-plugin
@tanstack/eslint-plugin-router
@tanstack/history
@tanstack/react-router
@tanstack/react-router-devtools
@tanstack/react-router-ssr-query
@tanstack/react-start
@tanstack/react-start-client
@tanstack/react-start-plugin
@tanstack/react-start-server
@tanstack/router-cli
@tanstack/router-core
@tanstack/router-devtools
@tanstack/router-devtools-core
@tanstack/router-generator
@tanstack/router-plugin
@tanstack/router-ssr-query-core
@tanstack/router-utils
@tanstack/router-vite-plugin
@tanstack/server-functions-plugin
@tanstack/solid-router
@tanstack/solid-router-devtools
@tanstack/solid-start
@tanstack/solid-start-client
@tanstack/solid-start-plugin
@tanstack/solid-start-server
@tanstack/start-client-core
@tanstack/start-plugin-core
@tanstack/start-server-core
@tanstack/start-server-functions-client
@tanstack/start-server-functions-fetcher
@tanstack/start-server-functions-server
@tanstack/start-storage-context
@tanstack/valibot-adapter
@tanstack/virtual-file-routes
@tanstack/zod-adapter
commit: |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/router-core/src/scroll-restoration.ts (1)
31-31: Consider preserving error context for debuggingSilently swallowing errors can make debugging harder in production environments. Consider at least logging the error to help with troubleshooting storage-related issues.
} catch { - // silent + // Storage access denied or unavailable
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
packages/router-core/src/scroll-restoration.ts(8 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Test
- GitHub Check: Preview
🔇 Additional comments (7)
packages/router-core/src/scroll-restoration.ts (7)
88-88: Good performance improvement with explicit typingThe explicit typing of
parentasHTMLElementimproves type safety and code clarity.
90-95: Clever optimization using push + reverse instead of unshiftGood performance optimization! Using
push()in the loop followed by a singlereverse()is more efficient than repeatedunshift()operations, asunshift()has O(n) complexity for each call whilepush()is O(1).
135-161: Smart refactor using labeled block instead of IIFEExcellent optimization! Replacing the IIFE with a labeled block eliminates the function call overhead while maintaining the same control flow semantics. This is a clean and performant approach.
167-167: Robust hash extraction with split limitGood defensive programming! Using
split('#', 2)ensures that only the first#is used as a delimiter, preventing issues with URLs that might contain multiple#characters in the hash portion.
185-196: Clean scroll-to-top implementationThe refactored implementation is more readable and maintainable. The use of a single
scrollOptionsobject and the explicitcontinuefor 'window' selector improves code clarity.
291-294: Efficient state initialization with ||= operatorGreat use of the logical OR assignment operator (
||=) for lazy initialization. This is both more concise and potentially more performant than the previous approach.
340-340: Consistent use of ||= operatorGood consistency with the ||= operator usage throughout the file. This maintains a uniform coding style.
…4990) Some minor performance improvements in `scroll-restoration.ts` - in `getCssSelector`: - we can obtain `indexOf` without creating an empty array through `Array.prototype.indexOf` - using `.unshift` in a loop is always bad for performance. To build arrays in reverse, use `.push()` in the loop, and `.reverse()` at the end. - in `restoreScroll`: - we can early exit without creating an IIFE by using a [labeled statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label) - we can avoid creating an array of elements to iterate over since we already have an array - in `onScroll`: - we can avoid accessing the same key twice by using the `||=` operator <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - None - Bug Fixes - More reliable scroll restoration after navigation and refresh. - Improved handling of URL hash anchors for accurate in-page scrolling. - Consistent scroll-to-top behavior across configured elements. - Avoids runtime errors when session storage is unavailable or data is invalid. - Refactor - Streamlined scroll handling and state initialization for improved stability and minor performance gains. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
…anStack#4990) Some minor performance improvements in `scroll-restoration.ts` - in `getCssSelector`: - we can obtain `indexOf` without creating an empty array through `Array.prototype.indexOf` - using `.unshift` in a loop is always bad for performance. To build arrays in reverse, use `.push()` in the loop, and `.reverse()` at the end. - in `restoreScroll`: - we can early exit without creating an IIFE by using a [labeled statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label) - we can avoid creating an array of elements to iterate over since we already have an array - in `onScroll`: - we can avoid accessing the same key twice by using the `||=` operator <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - None - Bug Fixes - More reliable scroll restoration after navigation and refresh. - Improved handling of URL hash anchors for accurate in-page scrolling. - Consistent scroll-to-top behavior across configured elements. - Avoids runtime errors when session storage is unavailable or data is invalid. - Refactor - Streamlined scroll handling and state initialization for improved stability and minor performance gains. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Some minor performance improvements in
scroll-restoration.tsgetCssSelector:indexOfwithout creating an empty array throughArray.prototype.indexOf.unshiftin a loop is always bad for performance. To build arrays in reverse, use.push()in the loop, and.reverse()at the end.restoreScroll:onScroll:||=operatorSummary by CodeRabbit