@@ -32,9 +32,11 @@ const toPrefetch = new Set();
3232 * @return {Boolean } If true, then it should be ignored
3333 */
3434function isIgnored ( node , filter ) {
35- return Array . isArray ( filter ) ?
36- filter . some ( x => isIgnored ( node , x ) ) :
37- ( filter . test || filter ) . call ( filter , node . href , node ) ;
35+ if ( Array . isArray ( filter ) ) {
36+ return filter . some ( x => isIgnored ( node , x ) ) ;
37+ }
38+
39+ return ( filter . test || filter ) . call ( filter , node . href , node ) ;
3840}
3941
4042/**
@@ -59,8 +61,8 @@ function isIgnored(node, filter) {
5961export function listen ( options = { } ) {
6062 if ( ! window . IntersectionObserver ) return ;
6163
62- const [ toAdd , isDone ] = throttle ( options . throttle || 1 / 0 ) ;
63- const limit = options . limit || 1 / 0 ;
64+ const [ toAdd , isDone ] = throttle ( options . throttle || Number . Infinity ) ;
65+ const limit = options . limit || Number . Infinity ;
6466
6567 const allowed = options . origins || [ location . hostname ] ;
6668 const ignores = options . ignores || [ ] ;
@@ -79,36 +81,37 @@ export function listen(options = {}) {
7981 } ;
8082
8183 const observer = new IntersectionObserver ( entries => {
82- entries . forEach ( entry => {
83- if ( entry . isIntersecting ) {
84- observer . unobserve ( entry = entry . target ) ;
85- // Do not prefetch if will match/exceed limit
86- if ( toPrefetch . size < limit ) {
87- toAdd ( ( ) => {
88- prefetchChunks ?
89- prefetchChunks ( entry , prefetchHandler ) :
90- prefetchHandler ( entry . href ) ;
91- } ) ;
92- }
84+ for ( const { isIntersecting , target } of entries ) {
85+ if ( ! isIntersecting ) continue ;
86+
87+ observer . unobserve ( target ) ;
88+ // Do not prefetch if will match/exceed limit
89+ if ( toPrefetch . size < limit ) {
90+ toAdd ( ( ) => {
91+ prefetchChunks ?
92+ prefetchChunks ( target , prefetchHandler ) :
93+ prefetchHandler ( target . href ) ;
94+ } ) ;
9395 }
94- } ) ;
96+ }
9597 } ) ;
9698
9799 timeoutFn ( ( ) => {
98100 // Find all links & Connect them to IO if allowed
99- ( options . el || document ) . querySelectorAll ( 'a' ) . forEach ( link => {
101+ const links = ( options . el || document ) . querySelectorAll ( 'a[href]' ) ;
102+ for ( const link of links ) {
100103 // If the anchor matches a permitted origin
101104 // ~> A `[]` or `true` means everything is allowed
102105 if ( ! allowed . length || allowed . includes ( link . hostname ) ) {
103106 // If there are any filters, the link must not match any of them
104107 if ( ! isIgnored ( link , ignores ) ) observer . observe ( link ) ;
105108 }
106- } ) ;
109+ }
107110 } , {
108111 timeout : options . timeout || 2000 ,
109112 } ) ;
110113
111- return function ( ) {
114+ return ( ) => {
112115 // wipe url list
113116 toPrefetch . clear ( ) ;
114117 // detach IO entries
@@ -124,30 +127,27 @@ export function listen(options = {}) {
124127 */
125128export function prefetch ( url , isPriority ) {
126129 const { connection} = navigator ;
130+ if ( ! connection ) return Promise . resolve ( ) ;
127131
128- if ( connection ) {
129- // Don't prefetch if using 2G or if Save-Data is enabled.
130- if ( connection . saveData ) {
131- return Promise . reject ( new Error ( 'Cannot prefetch, Save-Data is enabled' ) ) ;
132- }
132+ // Don't prefetch if using 2G or if Save-Data is enabled.
133+ if ( connection . saveData ) {
134+ return Promise . reject ( new Error ( 'Cannot prefetch, Save-Data is enabled' ) ) ;
135+ }
133136
134- if ( / 2 g / . test ( connection . effectiveType ) ) {
135- return Promise . reject ( new Error ( 'Cannot prefetch, network conditions are poor' ) ) ;
136- }
137+ if ( / 2 g / . test ( connection . effectiveType ) ) {
138+ return Promise . reject ( new Error ( 'Cannot prefetch, network conditions are poor' ) ) ;
137139 }
138140
139141 // Dev must supply own catch()
140142 return Promise . all (
141- [ ] . concat ( url ) . map ( str => {
143+ [ url ] . flat ( ) . map ( str => {
142144 if ( toPrefetch . has ( str ) ) return [ ] ;
143145
144146 // Add it now, regardless of its success
145147 // ~> so that we don't repeat broken links
146148 toPrefetch . add ( str ) ;
147149
148- return ( isPriority ? viaFetch : supported ) (
149- new URL ( str , location . href ) . toString ( ) ,
150- ) ;
150+ return ( isPriority ? viaFetch : supported ) ( new URL ( str , location . href ) . toString ( ) ) ;
151151 } ) ,
152152 ) ;
153153}
0 commit comments