fix: avoid firing unnecessary preload on focus event#1885
fix: avoid firing unnecessary preload on focus event#1885freshgiammi wants to merge 3 commits intoTanStack:mainfrom freshgiammi:fix_link_preload_focus
Conversation
☁️ Nx Cloud ReportCI is running/has finished running commands for commit cc35b11. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this CI Pipeline Execution ✅ Successfully ran 4 targetsSent with 💌 from NxCloud. |
|
what is the real problem with preloading twice? isn't this cached by router (depending on the stale / gc time) settings? |
|
Yeah, there's a couple points this tries to improve rather than fix:
It's more an improvement than a solution to a problem, but honestly it was screwing up my debugging on the preload issue because it kept changing the route state when I was not expecting it. If the preload finishes and the route gets cached sure it probably won't harm, but why the extra work (and inconsistent behaviour)? |
SeanCassiere
left a comment
There was a problem hiding this comment.
Should this have any event listeners for touch-based actions?
Hm, how do we express intent on mobile? Currently, I've tweaked the patch a bit further (and now it fires |
|
With Tanner's #1907, we'll be deduping the Once it goes through, we'll revisit the need for this one. |
|
This should no longer be needed since the route callbacks are now being deduped. @freshgiammi thoughts? I think we can close. |
I haven't been able to test the router after #1907 was merged. I'll take a look early next week and report back, but from a cursory glance I think this isn't problematic anymore: we're probably fine with doing extra function calls rather than attaching listeners to prevent them now that route matching and loading is deduped. |
|
@SeanCassiere I'm not entirely sure everything has been deduped: the behaviour I see is different on first preload vs consecutive navigation, with loader running multiple times at the same time (in different amounts). Considering the work done on route callback side, improvements should be made there instead of here. EDIT: Scratch that, it looks like it's an issue with how my router is set up. I can't replicate any of the preloading/context issues I've had in the past. |
onFocusis also invoked when a button is clicked, which causes the route to get preloaded twice (onMouseEnter+onFocus).When focusing a link via keyboard navigation, we obviously skip
onMouseEnterso it gets correctly preloaded once.Given that there's no way to distinguish between mouse and keyboard origin for focus events, we can "trap" it by skipping the preload call when a focus event is detected between
onMouseDownandonMouseUp: this makes preload only happen once and ensures that the behaviour is the same for both mouse and keyboard navigation.