@@ -49,15 +49,17 @@ function isIgnored(node, filter) {
4949 * @return {Boolean|Object } Error Object if the constrainsts are met or boolean otherwise
5050 */
5151function checkConnection ( conn ) {
52- if ( conn ) {
53- // Don't pre* if using 2G or if Save-Data is enabled.
54- if ( conn . saveData ) {
55- return new Error ( 'Save-Data is enabled' ) ;
56- }
52+ // If no connection object, assume it's okay to prefetch
53+ if ( ! conn ) return true ;
5754
58- if ( / 2 g / . test ( conn . effectiveType ) ) {
59- return new Error ( 'network conditions are poor' ) ;
60- }
55+ // Don't prefetch if Save-Data is enabled.
56+ if ( conn . saveData ) {
57+ return new Error ( 'Save-Data is enabled' ) ;
58+ }
59+
60+ // Don't prefetch if using 2G connection.
61+ if ( / 2 g / . test ( conn . effectiveType ) ) {
62+ return new Error ( 'network conditions are poor' ) ;
6163 }
6264
6365 return true ;
@@ -120,7 +122,7 @@ export function listen(options = {}) {
120122 } ;
121123
122124 const observer = new IntersectionObserver ( entries => {
123- entries . forEach ( entry => {
125+ for ( let entry of entries ) {
124126 // On enter
125127 if ( entry . isIntersecting ) {
126128 entry = entry . target ;
@@ -140,7 +142,11 @@ export function listen(options = {}) {
140142 // either it's the prerender + prefetch mode or it's prerender *only* mode
141143 // Prerendering limit is following options.limit. UA may impose arbitraty numeric limit
142144 // The same URL is not already present as a speculation rule
143- if ( ( shouldPrerenderAndPrefetch || shouldOnlyPrerender ) && toPrerender . size < limit && ! specRulesInViewport . has ( entry . href ) ) {
145+ if (
146+ ( shouldPrerenderAndPrefetch || shouldOnlyPrerender ) &&
147+ toPrerender . size < limit &&
148+ ! specRulesInViewport . has ( entry . href )
149+ ) {
144150 prerender ( hrefFn ? hrefFn ( entry ) : entry . href , options . eagerness )
145151 . then ( specMap => {
146152 for ( const [ key , value ] of specMap ) {
@@ -161,8 +167,13 @@ export function listen(options = {}) {
161167 // Do not prefetch if will match/exceed limit and user has not switched to shouldOnlyPrerender mode
162168 if ( toPrefetch . size < limit && ! shouldOnlyPrerender ) {
163169 toAdd ( ( ) => {
164- prefetch ( hrefFn ? hrefFn ( entry ) : entry . href , options . priority ,
165- options . checkAccessControlAllowOrigin , options . checkAccessControlAllowCredentials , options . onlyOnMouseover )
170+ prefetch (
171+ hrefFn ? hrefFn ( entry ) : entry . href ,
172+ options . priority ,
173+ options . checkAccessControlAllowOrigin ,
174+ options . checkAccessControlAllowCredentials ,
175+ options . onlyOnMouseover ,
176+ )
166177 . then ( isDone )
167178 . catch ( error => {
168179 isDone ( ) ;
@@ -176,41 +187,37 @@ export function listen(options = {}) {
176187 entry = entry . target ;
177188 const index = hrefsInViewport . indexOf ( entry . href ) ;
178189
179- if ( index > - 1 ) {
190+ if ( index !== - 1 ) {
180191 hrefsInViewport . splice ( index ) ;
181192 }
182193
183194 if ( specRulesInViewport . has ( entry . href ) ) {
184195 specRulesInViewport . set ( removeSpeculationRule ( specRulesInViewport , entry . href ) ) ;
185196 }
186197 }
187- } ) ;
198+ }
188199 } , {
189200 threshold,
190201 } ) ;
191202
192203 timeoutFn ( ( ) => {
193204 // Find all links & Connect them to IO if allowed
194- const elementsToListen = options . el &&
195- options . el . length &&
196- options . el . length > 0 &&
197- options . el [ 0 ] . nodeName === 'A' ?
198- options . el :
199- ( options . el || document ) . querySelectorAll ( 'a' ) ;
200-
201- elementsToListen . forEach ( link => {
205+ const isAnchorElement = options . el && options . el . length > 0 && options . el [ 0 ] . nodeName === 'A' ;
206+ const elementsToListen = isAnchorElement ? options . el : ( options . el || document ) . querySelectorAll ( 'a' ) ;
207+
208+ for ( const link of elementsToListen ) {
202209 // If the anchor matches a permitted origin
203210 // ~> A `[]` or `true` means everything is allowed
204211 if ( ! allowed . length || allowed . includes ( link . hostname ) ) {
205212 // If there are any filters, the link must not match any of them
206213 if ( ! isIgnored ( link , ignores ) ) observer . observe ( link ) ;
207214 }
208- } ) ;
215+ }
209216 } , {
210217 timeout : options . timeout || 2000 ,
211218 } ) ;
212219
213- return function ( ) {
220+ return ( ) => {
214221 // wipe url list
215222 toPrefetch . clear ( ) ;
216223 // detach IO entries
@@ -239,18 +246,22 @@ export function prefetch(urls, isPriority, checkAccessControlAllowOrigin, checkA
239246 }
240247
241248 // Dev must supply own catch()
242- return Promise . all (
243- [ ] . concat ( urls ) . map ( str => {
244- if ( toPrefetch . has ( str ) ) return [ ] ;
245-
246- // Add it now, regardless of its success
247- // ~> so that we don't repeat broken links
248- toPrefetch . add ( str ) ;
249-
250- return prefetchOnHover ( ( isPriority ? viaFetch : supported ) , new URL ( str , location . href ) . toString ( ) , onlyOnMouseover ,
251- checkAccessControlAllowOrigin , checkAccessControlAllowCredentials , isPriority ) ;
252- } ) ,
253- ) ;
249+ return Promise . all ( [ urls ] . flat ( ) . map ( str => {
250+ if ( toPrefetch . has ( str ) ) return [ ] ;
251+
252+ // Add it now, regardless of its success
253+ // ~> so that we don't repeat broken links
254+ toPrefetch . add ( str ) ;
255+
256+ return prefetchOnHover (
257+ isPriority ? viaFetch : supported ,
258+ new URL ( str , location . href ) . toString ( ) ,
259+ onlyOnMouseover ,
260+ checkAccessControlAllowOrigin ,
261+ checkAccessControlAllowCredentials ,
262+ isPriority ,
263+ ) ;
264+ } ) ) ;
254265}
255266
256267/**
@@ -260,7 +271,7 @@ export function prefetch(urls, isPriority, checkAccessControlAllowOrigin, checkA
260271* @return {Object } a Promise
261272*/
262273export function prerender ( urls , eagerness = 'immediate' ) {
263- urls = [ ] . concat ( urls ) ;
274+ urls = [ urls ] . flat ( ) ;
264275
265276 const chkConn = checkConnection ( navigator . connection ) ;
266277 if ( chkConn instanceof Error ) {
0 commit comments