@@ -29,21 +29,6 @@ const observer = new IntersectionObserver(entries => {
2929 loaderFunctions . get ( url ) . call ( null ) ;
3030 } ) ;
3131} ) ;
32- /**
33- * Prefetch in-viewport links from a target DOM element. The
34- * element will be observed using Intersection Observer.
35- * @param {Object } el DOM element to check for in-viewport links
36- * @param {Object } options Quicklink options object
37- * @return {Promise } resolving with list of URLs found
38- */
39- function fetchInViewportLinks ( el , options ) {
40- const links = Array . from ( el . querySelectorAll ( 'a' ) ) ;
41- links . forEach ( link => {
42- observer . observe ( link ) ;
43- } ) ;
44- // Return a list of found URLs
45- return links . map ( link => link . href ) ;
46- } ;
4732
4833/**
4934 * Prefetch an array of URLs if the user's effective
@@ -69,18 +54,24 @@ export default function (options) {
6954 ...options ,
7055 } ;
7156
72- if ( ! options . urls ) {
73- options . urls = fetchInViewportLinks ( options . el , options ) ;
74- }
57+ options . timeoutFn ( ( ) => {
58+ // If URLs are given, prefetch them.
59+ if ( options . urls ) {
60+ options . urls . forEach ( url => prefetch ( url , options . priority ) ) ;
61+ return ;
62+ }
63+
64+ // If not, find all links and use IntersectionObserver.
65+ const linkTags = Array . from ( options . el . querySelectorAll ( 'a' ) ) ;
66+ linkTags . forEach ( link => observer . observe ( link ) ) ;
67+ const urls = linkTags . map ( link => link . href ) ;
7568
76- options . urls . forEach ( url => {
77- loaderFunctions . set ( url , ( ) => {
78- loaderFunctions . delete ( url ) ;
79- prefetch ( url , options . priority ) ;
69+ // Generate loader functions for each link
70+ urls . forEach ( url => {
71+ loaderFunctions . set ( url , ( ) => {
72+ loaderFunctions . delete ( url ) ;
73+ prefetch ( url , options . priority ) ;
74+ } ) ;
8075 } ) ;
81- } ) ;
82- options . timeoutFn ( ( ) => {
83- // This is a bit weird, but somehow map access gets transpiled the wrong way.
84- Array . from ( loaderFunctions . values ( ) ) . forEach ( f => f ( ) ) ;
8576 } , { timeout : options . timeout } ) ;
8677}
0 commit comments