Skip to content

Commit

Permalink
Lots of ESLint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
addyosmani committed Nov 24, 2018
1 parent 6571ff0 commit 5d0af3d
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 86 deletions.
35 changes: 35 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = {
env: {
browser: true,
es6: true
},
parserOptions: {
sourceType: 'module',
},
extends: 'google',
rules: {
'max-len': ['warn',
{
code: 130
} // 130 on GitHub, 80 on npmjs.org for README.md code blocks
],
'arrow-parens': ['error', 'as-needed'],
'space-before-function-paren': [
'error',
{
anonymous: 'always',
named: 'never'
}
],
'no-negated-condition': 'warn',
'spaced-comment': ['error', 'always',
{
exceptions: ['/']
}
]
},
globals: {
DOMException: false,
URL: false,
}
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
"name": "quicklink",
"version": "0.0.1",
"description": "Faster page-loads by prefetching links during idle time",
"main": "src/index.mjs",
"main": "dist/quicklink.js",
"repository": "https://github.com/addyosmani/quicklink.git",
"author": "addyosmani <[email protected]>",
"license": "Apache-2.0",
"private": false,
"main": "dist/quicklink.js",
"module": "dist/quicklink.mjs",
"jsnext:main": "dist/quicklink.mjs",
"umd:main": "dist/quicklink.umd.js",
"scripts": {
"test": "eslint src",
"lint": "eslint src/*.mjs",
"build": "microbundle src/index.mjs --no-sourcemap",
"prepare": "npm run -s build"
},
"devDependencies": {
"eslint": "^5.9.0",
"eslint-config-google": "^0.11.0",
"microbundle": "^0.7.0"
}
}
109 changes: 58 additions & 51 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,75 @@ import prefetch from './prefetch.mjs';
import requestIdleCallback from './request-idle-callback.mjs';

/**
*
* @param urls Array of URLs to prefetch
* Prefetch an array of URLs using rel=prefetch
* if supported. Falls back to XHR otherwise.
* @param {Array} urls Array of URLs to prefetch
*/
function fetchLinks(urls) {
urls.forEach((url) => {
prefetch(url);
});
urls.forEach(url => {
prefetch(url);
});
}

/**
* Extract only links that are in the visible viewport
* @param el DOM element to check for in-viewport links
* Extract links from a provided DOM element that are
* in the user's viewport.
* @param {Object} el DOM element to check for in-viewport links
* @return {Promise} resolving with list of URLs found
*/
function extractInViewportLinks(el) {
return new Promise((resolve, reject) => {
const urls = [];
const links = el.querySelectorAll('a');
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
// Link is in the view
if (entry.intersectionRatio > 0) {
urls.push(entry.target.href);
} else {
// Link is out of the view
}
});
resolve(urls);
});
links.forEach(link => {
observer.observe(link);
});
return new Promise((resolve, reject) => {
const urls = [];
const links = el.querySelectorAll('a');
const observer = new IntersectionObserver(entries => {
entries.forEach(entry => {
// Link is in the view
if (entry.intersectionRatio > 0) {
urls.push(entry.target.href);
} else {
// Link is out of the view
}
});
resolve(urls);
});
links.forEach(link => {
observer.observe(link);
});
});
}

/**
*
* @param options
* Prefetch an array of URLs if the user's effective
* connection type and data-saver preferences suggests
* it would be useful. By default, looks at in-viewport
* links for `document`. Can also work off a supplied
* DOM element or static array of URLs.
* @param {Object} options supported options
* options.urls: Array of URLs to prefetch (override)
* options.el: DOM element to prefetch in-viewport links of
*/
export default function quicklink(options) {
options = options || {};
requestIdleCallback(() => {
if ('connection' in navigator) {
// Don't prefetch if the user is on 2G..
if (navigator.connection.effectiveType && /\slow-2g|2g/.test(navigator.connection.effectiveType)) {
return;
}
// Don't prefetch if Save-Data is enabled..
if (navigator.connection.saveData) {
return;
}
}
// Prefetch an array of URLs if supplied (as an override)
if (options.urls !== undefined && options.urls.length > 0) {
fetchLinks(options.urls);
} else {
// Element to extract in-viewport links for
let el = options.el || document;
extractInViewportLinks(el).then((urls) => {
fetchLinks(urls);
});
}
});
}
export default function (options) {
options = options || {};
requestIdleCallback(() => {
if ('connection' in navigator) {
// Don't prefetch if the user is on 2G..
if (navigator.connection.effectiveType && /\slow-2g|2g/.test(navigator.connection.effectiveType)) {
return;
}
// Don't prefetch if Save-Data is enabled..
if (navigator.connection.saveData) {
return;
}
}
// Prefetch an array of URLs if supplied (as an override)
if (options.urls !== undefined && options.urls.length > 0) {
fetchLinks(options.urls);
} else {
// Element to extract in-viewport links for
const el = options.el || document;
extractInViewportLinks(el).then(urls => {
fetchLinks(urls);
});
}
});
}
56 changes: 28 additions & 28 deletions src/prefetch.mjs
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
const support = function(feature) {
const support = function (feature) {
if (typeof document === `undefined`) {
return false
return false;
}
const fakeLink = document.createElement(`link`)
const fakeLink = document.createElement(`link`);
try {
if (fakeLink.relList && typeof fakeLink.relList.supports === `function`) {
return fakeLink.relList.supports(feature)
return fakeLink.relList.supports(feature);
}
} catch (err) {
return false
return false;
}
}
const linkPrefetchStrategy = function(url) {
};
const linkPrefetchStrategy = function (url) {
if (typeof document === `undefined`) {
return
return;
}
const link = document.createElement(`link`)
link.setAttribute(`rel`, `prefetch`)
link.setAttribute(`href`, url)
const link = document.createElement(`link`);
link.setAttribute(`rel`, `prefetch`);
link.setAttribute(`href`, url);
const parentElement =
document.getElementsByTagName(`head`)[0] ||
document.getElementsByName(`script`)[0].parentNode
parentElement.appendChild(link)
}
const xhrPrefetchStrategy = function(url) {
const req = new XMLHttpRequest()
req.open(`GET`, url, true)
req.withCredentials = true
req.send(null)
}
document.getElementsByName(`script`)[0].parentNode;
parentElement.appendChild(link);
};
const xhrPrefetchStrategy = function (url) {
const req = new XMLHttpRequest();
req.open(`GET`, url, true);
req.withCredentials = true;
req.send(null);
};

const supportedPrefetchStrategy = support(`prefetch`)
? linkPrefetchStrategy
: xhrPrefetchStrategy
: xhrPrefetchStrategy;

const preFetched = {}
const preFetched = {};

const prefetch = function(url) {
const prefetch = function (url) {
if (preFetched[url]) {
return
return;
}
preFetched[url] = true
supportedPrefetchStrategy(url)
}
preFetched[url] = true;
supportedPrefetchStrategy(url);
};

export default prefetch
export default prefetch;
8 changes: 4 additions & 4 deletions src/request-idle-callback.mjs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// RIC and shim for browsers setTimeout() without it
const requestIdleCallback = window.requestIdleCallback ||
function (cb) {
let start = Date.now();
const start = Date.now();
return setTimeout(function () {
cb({
didTimeout: false,
timeRemaining: function () {
return Math.max(0, 50 - (Date.now() - start));
}
},
});
}, 1);
}
};

export default requestIdleCallback;
export default requestIdleCallback;
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,10 @@ escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"

eslint-config-google@^0.11.0:
version "0.11.0"
resolved "https://registry.yarnpkg.com/eslint-config-google/-/eslint-config-google-0.11.0.tgz#fd0fc70be2e9114df097cac93a9b5b0d4e1c6830"

eslint-scope@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-4.0.0.tgz#50bf3071e9338bcdc43331794a0cb533f0136172"
Expand Down

0 comments on commit 5d0af3d

Please sign in to comment.