-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
refactor(router-core): scroll-restoration minor performance cleanup #4990
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
base: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
parent
asHTMLElement
improves 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
scrollOptions
object and the explicitcontinue
for '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.
Some minor performance improvements in
scroll-restoration.ts
getCssSelector
:indexOf
without creating an empty array throughArray.prototype.indexOf
.unshift
in 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