Skip to content

Commit

Permalink
Use mouseenter and mouseleave instead of mouseover and mouseout
Browse files Browse the repository at this point in the history
To avoid having to traverse the DOM to find the link element
  • Loading branch information
davidalejandroaguilar committed Dec 9, 2023
1 parent 4170ddb commit 5078e0b
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/observers/link_prefetch_observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import {
doesNotTargetIFrame,
getLocationForLink,
getMetaContent,
findLinkFromClickTarget,
findClosestRecursively
} from "../util"
import { prefetchCache, cacheTtl } from "../core/drive/prefetch_cache"

export class LinkPrefetchObserver {
triggerEvents = {
mouseover: "mouseover",
mouseover: "mouseenter",
mousedown: "mousedown"
}
started = false
Expand Down Expand Up @@ -55,17 +54,18 @@ export class LinkPrefetchObserver {
if (getMetaContent("turbo-prefetch") !== "true") return

const target = event.target
const link = findLinkFromClickTarget(target)
const isLink = target.matches && target.matches("a[href]:not([target^=_]):not([download])")
const link = target

if (link && this.#isPrefetchable(link)) {
if (isLink && this.#isPrefetchable(link)) {
const delay = target.dataset.turboPrefetchDelay || getMetaContent("turbo-prefetch-delay")

if (delay) {
this.prefetchTimeout = setTimeout(() => {
this.#startPrefetch(event, link)
} , Number(delay))

link.addEventListener('mouseout', this.#cancelPrefetchTimeoutIfAny, {
link.addEventListener('mouseleave', this.#cancelPrefetchTimeoutIfAny, {
capture: true,
passive: true
})
Expand Down

0 comments on commit 5078e0b

Please sign in to comment.