-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add support to avoid scroll position calculation on router push #2393
Comments
What about not saving the position if no scrollBehavior is provided? |
I am not sure if I understand how not to save the position, but I will try to answer. I've configured router with createRouter({
history: createWebHashHistory(),
base: __dirname,
scrollBehavior () {
return false
},
routes: [
{},
// ...
]
}) @posva Did you mean this way? |
No, I mean that if no |
Ok, understand. This was my initial setup, without |
@ferferga Thanks for clarification. Unfortunately same behaviour happens, |
it won't work until it's implemented, that's what I mean by "if no scrollBehavior option is passed, the router could ...". But it's okay, I was planning on implementing it this way already 😄 |
After one more iteration it is absolutely clear. 😂 That's great, there is no need for API changes. |
What problem is this solving
Description
Vue Router currently calculates the scroll position on every router.push navigation by invoking the computeScrollPosition function, which accesses window.pageXOffset and window.pageYOffset. While this behavior is useful for maintaining scroll position, it triggers layout recalculations that can negatively impact performance, especially on low-powered devices.
Given that JavaScript applications built with Vue.js can run across a variety of platforms — from mobile phones to high-performance computers, embedded devices, and Smart TVs — it's essential to account for environments where layout-intensive operations can hinder the user experience.
Motivation
The layout calculation triggered by accessing scroll-related properties is particularly expensive on embedded devices and Smart TVs, where hardware constraints may limit performance. Reducing unnecessary layout operations is critical for ensuring smooth navigation and an optimal user experience on these platforms.
Applications on such devices couldn't use scroll behaviour at all and such feature is redudant for these platforms.
Currently, Vue Router does not offer a way to skip or control these scroll position calculations, resulting in performance overhead even when maintaining scroll position is unnecessary.
Proposed solution
We propose extending the Vue Router API with an option to disable scroll position calculations on navigation. This could be configured either globally or on a per-route basis, providing developers with more control over performance-critical scenarios.
This feature would ensure that developers working on performance-sensitive applications — especially for embedded systems or Smart TVs — can reduce the performance impact associated with layout recalculations.
Another alternative solution could be to add third parameter to
push
function to avoid scroll position computation, like it has thebuildState
function.Describe alternatives you've considered
A workaround could be manually overriding the
window.pageXOffset
andwindow.pageYOffset
properties to skip layout calculations. However, this approach, modifying window object, could be error prone in the context of entire applications.The text was updated successfully, but these errors were encountered: