Skip to content

Commit

Permalink
prefetching on mobile using 'touchstart'
Browse files Browse the repository at this point in the history
  • Loading branch information
gijo-varghese committed Sep 13, 2019
1 parent 1ae7b16 commit e5ef253
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions flying-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ const mouseOverListener = event => {
}
};

// Preload on touchstart on mobile
const touchStartListener = event => {
const elm = event.target.closest("a");
if (elm && elm.href && !alreadyPrefetched.has(elm.href))
addUrlToQueue(elm.href, true);
};

// Clear timeout on mouse out if not already preloaded
const mouseOutListener = event => {
const elm = event.target.closest("a");
Expand Down Expand Up @@ -133,9 +140,10 @@ const stopPreloading = () => {
// Clear pending links in queue
toPrefetch.clear();

// Remove event listeners for mouse hover
// Remove event listeners for mouse hover and mobile touch
document.removeEventListener("mouseover", mouseOverListener, true);
document.removeEventListener("mouseout", mouseOutListener, true);
document.removeEventListener("touchstart", touchStartListener, true);
};

function flyingPages(options = {}) {
Expand Down Expand Up @@ -164,12 +172,9 @@ function flyingPages(options = {}) {
)
);

// Add event listeners to detect mouse hover
const eventListenerOptions = { capture: true, passive: true };
document.addEventListener(
"mouseover",
mouseOverListener,
eventListenerOptions
);
document.addEventListener("mouseout", mouseOutListener, eventListenerOptions);
// Add event listeners to detect mouse hover and mobile touch
const listenerOptions = { capture: true, passive: true };
document.addEventListener("mouseover", mouseOverListener, listenerOptions);
document.addEventListener("mouseout", mouseOutListener, listenerOptions);
document.addEventListener("touchstart", touchStartListener, listenerOptions);
}
2 changes: 1 addition & 1 deletion flying-pages.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e5ef253

Please sign in to comment.