Skip to content

Commit

Permalink
disable on IE and rename observer object
Browse files Browse the repository at this point in the history
  • Loading branch information
gijo-varghese committed Sep 28, 2019
1 parent 07cfbb8 commit 8060e53
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
16 changes: 11 additions & 5 deletions flying-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const isSlowConnection =
(navigator.connection.saveData ||
(navigator.connection.effectiveType || "").includes("2g"));

// Check browser is IE
const isIE =
navigator.userAgent.indexOf("MSIE ") > -1 ||
navigator.userAgent.indexOf("Trident/") > -1;

// Prefetch the given url using native 'prefetch'. Fallback to 'xhr' if not supported
const prefetch = url => {
// Prefetch using native prefetch
Expand Down Expand Up @@ -69,7 +74,7 @@ const addUrlToQueue = (url, processImmediately = false) => {
};

// Observe the links in viewport, add url to queue if found intersecting
const observer = new IntersectionObserver(entries => {
const linksObserver = new IntersectionObserver(entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const url = entry.target.href;
Expand Down Expand Up @@ -135,7 +140,7 @@ const requestIdleCallback =
// Stop preloading in case server is responding slow/errors
const stopPreloading = () => {
// Find all links are remove it from observer (viewport)
document.querySelectorAll("a").forEach(e => observer.unobserve(e));
document.querySelectorAll("a").forEach(e => linksObserver.unobserve(e));

// Clear pending links in queue
toPrefetch.clear();
Expand All @@ -147,8 +152,8 @@ const stopPreloading = () => {
};

function flyingPages(options = {}) {
// Don't start preloading if user is on a slow connection
if (isSlowConnection) return;
// Don't start preloading if user is on a slow connection or browser is IE
if (isSlowConnection || isIE) return;

// Default options incase options is not set
const defaultOptions = {
Expand All @@ -167,7 +172,8 @@ function flyingPages(options = {}) {
// Start preloading links in viewport on idle callback, with a delay
requestIdleCallback(() =>
setTimeout(
() => document.querySelectorAll("a").forEach(e => observer.observe(e)),
() =>
document.querySelectorAll("a").forEach(e => linksObserver.observe(e)),
window.FPConfig.delay * 1000
)
);
Expand Down
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 8060e53

Please sign in to comment.