@@ -23,14 +23,16 @@ const observer = new IntersectionObserver(entries => {
2323 entries . forEach ( entry => {
2424 if ( entry . isIntersecting ) {
2525 const url = entry . target . href ;
26- if ( toPrefetch . has ( url ) ) {
27- toPrefetch . delete ( url ) ;
28- prefetch ( url , observer . priority ) ;
29- }
26+ if ( toPrefetch . has ( url ) ) prefetcher ( url ) ;
3027 }
3128 } ) ;
3229} ) ;
3330
31+ function prefetcher ( url ) {
32+ toPrefetch . delete ( url ) ;
33+ return prefetch ( url , observer . priority ) ;
34+ }
35+
3436/**
3537 * Prefetch an array of URLs if the user's effective
3638 * connection type and data-saver preferences suggests
@@ -52,19 +54,18 @@ export default function (options) {
5254 el : document ,
5355 } , options ) ;
5456
57+ observer . priority = options . priority ;
58+
5559 options . timeoutFn ( ( ) => {
5660 // If URLs are given, prefetch them.
5761 if ( options . urls ) {
58- options . urls . forEach ( url => prefetch ( url , options . priority ) ) ;
59- return ;
62+ options . urls . forEach ( prefetcher ) ;
63+ } else {
64+ // If not, find all links and use IntersectionObserver.
65+ Array . from ( options . el . querySelectorAll ( 'a' ) , link => {
66+ observer . observe ( link ) ;
67+ toPrefetch . add ( link . href ) ;
68+ } ) ;
6069 }
61-
62- observer . priority = options . priority ;
63-
64- // If not, find all links and use IntersectionObserver.
65- Array . from ( options . el . querySelectorAll ( 'a' ) , link => {
66- observer . observe ( link ) ;
67- toPrefetch . add ( link . href ) ;
68- } ) ;
6970 } , { timeout : options . timeout } ) ;
7071}
0 commit comments