Comparing version 0.0.1 to 0.0.2
module.exports = { | ||
env: { | ||
browser: true, | ||
es6: true | ||
es6: true, | ||
node: true | ||
}, | ||
parserOptions: { | ||
sourceType: 'module', | ||
ecmaVersion: 8 | ||
}, | ||
@@ -9,0 +11,0 @@ extends: 'google', |
@@ -1,1 +0,1 @@ | ||
var n=function(n){return new Promise(function(e,t){var o=new XMLHttpRequest;o.open("GET",n,!0),o.withCredentials=!0,o.onload=function(){200===o.status?e():t()},o.send(null)})},e=function(n){if("undefined"==typeof document)return!1;var e=document.createElement("link");try{if(e.relList&&"function"==typeof e.relList.supports)return e.relList.supports(n)}catch(n){return!1}}("prefetch")?function(n){return new Promise(function(e,t){if("undefined"!=typeof document){var o=document.createElement("link");o.setAttribute("rel","prefetch"),o.setAttribute("href",n),o.onload=e,o.onerror=t,(document.getElementsByTagName("head")[0]||document.getElementsByName("script")[0].parentNode).appendChild(o)}else t()})}:n,t={},o=function(o,i){return new Promise(function(r){if("connection"in navigator){if(navigator.connection.effectiveType&&/\slow-2g|2g/.test(navigator.connection.effectiveType))return void r();if(navigator.connection.saveData)return void r()}t[o]?r():i&&"high"===i?function(e){return new Promise(function(t,o){void 0===self.fetch?n(e).then(function(){t()}):fetch(e,{credentials:"include"}).then(function(){t()})})}(o).then(function(){r(),t[o]=!0}).catch(function(){}):e(o).then(function(){r(),t[o]=!0}).catch(function(){})})},i=window.requestIdleCallback||function(n){var e=Date.now();return setTimeout(function(){n({didTimeout:!1,timeRemaining:function(){return Math.max(0,50-(Date.now()-e))}})},1)};var r=function(n,e){n.forEach(function(n){o(n,e)})};module.exports=function(n){n=n||{priority:"low"},i(function(){void 0!==n.urls&&n.urls.length>0?r(n.urls,n.priority):function(n){return new Promise(function(e,t){var o=[],i=n.querySelectorAll("a"),r=new IntersectionObserver(function(n){n.forEach(function(n){n.intersectionRatio>0&&o.push(n.target.href)}),e(o)});i.forEach(function(n){r.observe(n)})})}(n.el||document).then(function(e){r(e,n.priority)})})}; | ||
var n=function(n){return new Promise(function(t,e){var o=new XMLHttpRequest;o.open("GET",n,!0),o.withCredentials=!0,o.onload=function(){200===o.status?t():e()},o.send(null)})},t=function(n){if("undefined"==typeof document)return!1;var t=document.createElement("link");try{if(t.relList&&"function"==typeof t.relList.supports)return t.relList.supports(n)}catch(n){return!1}}("prefetch")?function(n){return new Promise(function(t,e){if("undefined"!=typeof document){var o=document.createElement("link");o.setAttribute("rel","prefetch"),o.setAttribute("href",n),o.onload=t,o.onerror=e,(document.getElementsByTagName("head")[0]||document.getElementsByName("script")[0].parentNode).appendChild(o)}else e()})}:n,e={},o=function(o,i){return new Promise(function(r){if("connection"in navigator){if(navigator.connection.effectiveType&&/\slow-2g|2g/.test(navigator.connection.effectiveType))return void r();if(navigator.connection.saveData)return void r()}e[o]?r():i&&"high"===i?function(t){return new Promise(function(e,o){void 0===self.fetch?n(t).then(function(){e()}):fetch(t,{credentials:"include"}).then(function(){e()})})}(o).then(function(){r(),e[o]=!0}).catch(function(){}):t(o).then(function(){r(),e[o]=!0}).catch(function(){})})},i=i||function(n){var t=Date.now();return setTimeout(function(){n({didTimeout:!1,timeRemaining:function(){return Math.max(0,50-(Date.now()-t))}})},1)};var r=function(n,t){n.forEach(function(n){o(n,t)})};module.exports=function(n){return new Promise(function(t,e){i(function(){void 0!==n.urls&&n.urls.length>0?(r(n.urls,n.priority),t(n.urls)):function(n){return new Promise(function(t,e){var o=[],i=n.querySelectorAll("a"),r=new IntersectionObserver(function(n){n.forEach(function(n){n.intersectionRatio>0&&o.push(n.target.href)}),t(o)});i.forEach(function(n){r.observe(n)})})}(n.el||document).then(function(e){r(e,n.priority),t(e)})},{timeout:(n=n||{priority:"low",timeout:2e3}).timeout})})}; |
@@ -1,1 +0,1 @@ | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):n.quicklink=e()}(this,function(){var n=function(n){return new Promise(function(e,t){var o=new XMLHttpRequest;o.open("GET",n,!0),o.withCredentials=!0,o.onload=function(){200===o.status?e():t()},o.send(null)})},e=function(n){if("undefined"==typeof document)return!1;var e=document.createElement("link");try{if(e.relList&&"function"==typeof e.relList.supports)return e.relList.supports(n)}catch(n){return!1}}("prefetch")?function(n){return new Promise(function(e,t){if("undefined"!=typeof document){var o=document.createElement("link");o.setAttribute("rel","prefetch"),o.setAttribute("href",n),o.onload=e,o.onerror=t,(document.getElementsByTagName("head")[0]||document.getElementsByName("script")[0].parentNode).appendChild(o)}else t()})}:n,t={},o=function(o,i){return new Promise(function(r){if("connection"in navigator){if(navigator.connection.effectiveType&&/\slow-2g|2g/.test(navigator.connection.effectiveType))return void r();if(navigator.connection.saveData)return void r()}t[o]?r():i&&"high"===i?function(e){return new Promise(function(t,o){void 0===self.fetch?n(e).then(function(){t()}):fetch(e,{credentials:"include"}).then(function(){t()})})}(o).then(function(){r(),t[o]=!0}).catch(function(){}):e(o).then(function(){r(),t[o]=!0}).catch(function(){})})},i=window.requestIdleCallback||function(n){var e=Date.now();return setTimeout(function(){n({didTimeout:!1,timeRemaining:function(){return Math.max(0,50-(Date.now()-e))}})},1)};var r=function(n,e){n.forEach(function(n){o(n,e)})};return function(n){n=n||{priority:"low"},i(function(){void 0!==n.urls&&n.urls.length>0?r(n.urls,n.priority):function(n){return new Promise(function(e,t){var o=[],i=n.querySelectorAll("a"),r=new IntersectionObserver(function(n){n.forEach(function(n){n.intersectionRatio>0&&o.push(n.target.href)}),e(o)});i.forEach(function(n){r.observe(n)})})}(n.el||document).then(function(e){r(e,n.priority)})})}}); | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):n.quicklink=e()}(this,function(){var n=function(n){return new Promise(function(e,t){var o=new XMLHttpRequest;o.open("GET",n,!0),o.withCredentials=!0,o.onload=function(){200===o.status?e():t()},o.send(null)})},e=function(n){if("undefined"==typeof document)return!1;var e=document.createElement("link");try{if(e.relList&&"function"==typeof e.relList.supports)return e.relList.supports(n)}catch(n){return!1}}("prefetch")?function(n){return new Promise(function(e,t){if("undefined"!=typeof document){var o=document.createElement("link");o.setAttribute("rel","prefetch"),o.setAttribute("href",n),o.onload=e,o.onerror=t,(document.getElementsByTagName("head")[0]||document.getElementsByName("script")[0].parentNode).appendChild(o)}else t()})}:n,t={},o=function(o,i){return new Promise(function(r){if("connection"in navigator){if(navigator.connection.effectiveType&&/\slow-2g|2g/.test(navigator.connection.effectiveType))return void r();if(navigator.connection.saveData)return void r()}t[o]?r():i&&"high"===i?function(e){return new Promise(function(t,o){void 0===self.fetch?n(e).then(function(){t()}):fetch(e,{credentials:"include"}).then(function(){t()})})}(o).then(function(){r(),t[o]=!0}).catch(function(){}):e(o).then(function(){r(),t[o]=!0}).catch(function(){})})},i=i||function(n){var e=Date.now();return setTimeout(function(){n({didTimeout:!1,timeRemaining:function(){return Math.max(0,50-(Date.now()-e))}})},1)};var r=function(n,e){n.forEach(function(n){o(n,e)})};return function(n){return new Promise(function(e,t){i(function(){void 0!==n.urls&&n.urls.length>0?(r(n.urls,n.priority),e(n.urls)):function(n){return new Promise(function(e,t){var o=[],i=n.querySelectorAll("a"),r=new IntersectionObserver(function(n){n.forEach(function(n){n.intersectionRatio>0&&o.push(n.target.href)}),e(o)});i.forEach(function(n){r.observe(n)})})}(n.el||document).then(function(t){r(t,n.priority),e(t)})},{timeout:(n=n||{priority:"low",timeout:2e3}).timeout})})}}); |
{ | ||
"name": "quicklink", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Faster subsequent page-loads by prefetching in-viewport links during idle time", | ||
@@ -13,6 +13,9 @@ "main": "dist/quicklink.js", | ||
"scripts": { | ||
"lint": "eslint src/*.mjs", | ||
"lint": "eslint src/*.mjs test/*.js", | ||
"lint-fix": "eslint src/*.mjs test/*.js --fix", | ||
"server": "http-server .", | ||
"test": "npm run build && mocha test/bootstrap.js --recursive test", | ||
"build": "microbundle src/index.mjs --no-sourcemap", | ||
"prepare": "npm run -s build", | ||
"release": "cross-var npm run build -s && cross-var git commit -am $npm_package_version && cross-var git tag $npm_package_version && git push && git push --tags && npm publish" | ||
"release": "cross-var npm run build -s && cross-var git commit -am $npm_package_version && cross-var git tag $npm_package_version && git push && git push --tags && npm publish --access public" | ||
}, | ||
@@ -28,8 +31,15 @@ "keywords": [ | ||
"devDependencies": { | ||
"babel-preset-env": "^1.7.0", | ||
"chai": "^4.2.0", | ||
"cross-var": "^1.1.0", | ||
"eslint": "^5.9.0", | ||
"eslint-config-google": "^0.11.0", | ||
"microbundle": "^0.7.0" | ||
"http-server": "^0.11.1", | ||
"lodash": "^4.17.11", | ||
"microbundle": "^0.7.0", | ||
"mocha": "^5.2.0" | ||
}, | ||
"dependencies": {} | ||
"dependencies": { | ||
"puppeteer": "^1.10.0" | ||
} | ||
} |
@@ -6,3 +6,3 @@ # quicklink | ||
Quicklink makes subsequent navigations to pages load faster. It: | ||
Quicklink attempts to make navigations to subsequent pages load faster. It: | ||
@@ -14,5 +14,14 @@ * **Detects links within the viewport** (using `IntersectionObsever`) | ||
## Installation | ||
For use with [node](http://nodejs.org) and [npm](https://npmjs.com): | ||
```sh | ||
npm install --save quicklink | ||
``` | ||
## Usage | ||
Once initialized, `quicklink` will automatically prefetch URLs for links that are in-viewport during idle time. | ||
Quickstart: | ||
@@ -36,4 +45,44 @@ | ||
The above options are best for multi-page sites. Single-page apps have a few options available for using quicklink with a router: | ||
* Call `quicklink()` once a navigation to a new route has completed | ||
* Call `quicklink()` against a specific DOM element / component | ||
* Call `quicklink({urls:[...]})` with a custom set of URLs to prefetch | ||
## API | ||
`quicklink` accepts an optional options object with the following parameters: | ||
* `el`: DOM element to observe for in-viewport links to prefetch | ||
* `urls`: Static array of URLs to prefetch (instead of observing `document` or a DOM element links in the viewport) | ||
* `timeout`: Integer for the `requestIdleCallback` timeout. A time in milliseconds by which the browser must execute prefetching. Defaults to 2 seconds. | ||
* `priority`: String specifying preferred priority for fetches. Defaults to `low`. `high` will attempt to use the `fetch()` API where supported (rather than rel=prefetch) | ||
TODO: | ||
* Explore detecting file-extension of resources and using [rel=preload](https://w3c.github.io/preload/) for `high` priority fetches | ||
* Explore using [Priority Hints](https://github.com/WICG/priority-hints) for importance hinting | ||
## Polyfills | ||
`quicklink`: | ||
* Includes a very small fallback for [requestIdleCallback](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestIdleCallback) | ||
* Requires `IntersectionObserver` to be supported (see [CanIUse](https://caniuse.com/#feat=intersectionobserver)). We recommend conditionally polyfillng this feature with a service like Polyfill.io: | ||
```html | ||
<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script> | ||
``` | ||
## Recipes | ||
**Set a custom timeout for prefetching resources** | ||
Defaults to 2 seconds (via `requestIdleCallback`). Here we override it to 4 seconds: | ||
```js | ||
quicklink({ | ||
timeout: 4000 | ||
}); | ||
``` | ||
**Set the DOM element to obseve for in-viewport links** | ||
@@ -44,3 +93,3 @@ | ||
```js | ||
let elem = document.getElementById('stuff'); | ||
const elem = document.getElementById('carousel'); | ||
quicklink({ | ||
@@ -57,3 +106,3 @@ el: elem | ||
quicklink({ | ||
urls: ['2.html','4.html'] | ||
urls: ['2.html','3.html', '4.js'] | ||
}); | ||
@@ -65,3 +114,3 @@ ``` | ||
Defaults to low-priority (`rel=prefetch` or XHR). For high-priority, | ||
attempts to use fetch() or falls back to XHR. | ||
attempts to use `fetch()` or falls back to XHR. | ||
@@ -68,0 +117,0 @@ ```js |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
197138
26
360
119
1
9
+ Addedpuppeteer@^1.10.0
+ Addedagent-base@4.3.0(transitive)
+ Addedasync-limiter@1.0.1(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbuffer-crc32@0.2.13(transitive)
+ Addedbuffer-from@1.1.2(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedconcat-stream@1.6.2(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddebug@2.6.93.2.74.3.7(transitive)
+ Addedes6-promise@4.2.8(transitive)
+ Addedes6-promisify@5.0.0(transitive)
+ Addedextract-zip@1.7.0(transitive)
+ Addedfd-slicer@1.1.0(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedhttps-proxy-agent@2.2.4(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedmime@2.6.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminimist@1.2.8(transitive)
+ Addedmkdirp@0.5.6(transitive)
+ Addedms@2.0.02.1.3(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedpend@1.2.0(transitive)
+ Addedprocess-nextick-args@2.0.1(transitive)
+ Addedprogress@2.0.3(transitive)
+ Addedproxy-from-env@1.1.0(transitive)
+ Addedpuppeteer@1.20.0(transitive)
+ Addedreadable-stream@2.3.8(transitive)
+ Addedrimraf@2.7.1(transitive)
+ Addedsafe-buffer@5.1.2(transitive)
+ Addedstring_decoder@1.1.1(transitive)
+ Addedtypedarray@0.0.6(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedws@6.2.3(transitive)
+ Addedyauzl@2.10.0(transitive)