-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6571ff0
commit 5d0af3d
Showing
6 changed files
with
132 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters