File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22 "name" : " quicklink" ,
33 "version" : " 0.0.1" ,
44 "description" : " Faster page-loads by prefetching links during idle time" ,
5- "main" : " index.js " ,
5+ "main" : " src/ index.mjs " ,
66 "repository" : " https://github.com/addyosmani/quicklink.git" ,
77 "author" : " addyosmani <addyosmani@gmail.com>" ,
88 "license" : " Apache-2.0" ,
Original file line number Diff line number Diff line change 1+ import prefetch from './prefetch.mjs' ;
2+ import requestIdleCallback from './request-idle-callback.mjs' ;
3+
14/**
25 *
36 * @param urls Array of URLs to prefetch
@@ -63,6 +66,4 @@ export default function quicklink(options) {
6366 } ) ;
6467 }
6568 } ) ;
66- }
67-
68- // TODO: add preload / high prio?
69+ }
Original file line number Diff line number Diff line change 1+ const support = function ( feature ) {
2+ if ( typeof document === `undefined` ) {
3+ return false
4+ }
5+ const fakeLink = document . createElement ( `link` )
6+ try {
7+ if ( fakeLink . relList && typeof fakeLink . relList . supports === `function` ) {
8+ return fakeLink . relList . supports ( feature )
9+ }
10+ } catch ( err ) {
11+ return false
12+ }
13+ }
14+ const linkPrefetchStrategy = function ( url ) {
15+ if ( typeof document === `undefined` ) {
16+ return
17+ }
18+ const link = document . createElement ( `link` )
19+ link . setAttribute ( `rel` , `prefetch` )
20+ link . setAttribute ( `href` , url )
21+ const parentElement =
22+ document . getElementsByTagName ( `head` ) [ 0 ] ||
23+ document . getElementsByName ( `script` ) [ 0 ] . parentNode
24+ parentElement . appendChild ( link )
25+ }
26+ const xhrPrefetchStrategy = function ( url ) {
27+ const req = new XMLHttpRequest ( )
28+ req . open ( `GET` , url , true )
29+ req . withCredentials = true
30+ req . send ( null )
31+ }
32+
33+ const supportedPrefetchStrategy = support ( `prefetch` )
34+ ? linkPrefetchStrategy
35+ : xhrPrefetchStrategy
36+
37+ const preFetched = { }
38+
39+ const prefetch = function ( url ) {
40+ if ( preFetched [ url ] ) {
41+ return
42+ }
43+ preFetched [ url ] = true
44+ supportedPrefetchStrategy ( url )
45+ }
46+
47+ export default prefetch
Original file line number Diff line number Diff line change 1+ // RIC and shim for browsers setTimeout() without it
2+ const requestIdleCallback = window . requestIdleCallback ||
3+ function ( cb ) {
4+ let start = Date . now ( ) ;
5+ return setTimeout ( function ( ) {
6+ cb ( {
7+ didTimeout : false ,
8+ timeRemaining : function ( ) {
9+ return Math . max ( 0 , 50 - ( Date . now ( ) - start ) ) ;
10+ }
11+ } ) ;
12+ } , 1 ) ;
13+ }
14+
15+ export default requestIdleCallback ;
You can’t perform that action at this time.
0 commit comments