File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1717import prefetch from './prefetch.mjs' ;
1818import requestIdleCallback from './request-idle-callback.mjs' ;
1919
20- const loaderFunctions = new Map ( ) ;
20+ const toPrefetch = new Set ( ) ;
21+
2122const observer = new IntersectionObserver ( entries => {
2223 entries . forEach ( entry => {
2324 if ( entry . isIntersecting ) {
2425 const url = entry . target . href ;
25- const fn = loaderFunctions . get ( url ) ;
26- if ( fn ) fn . call ( null ) ;
26+ if ( toPrefetch . has ( url ) ) {
27+ toPrefetch . delete ( url ) ;
28+ prefetch ( url , observer . priority ) ;
29+ }
2730 }
2831 } ) ;
2932} ) ;
@@ -56,16 +59,12 @@ export default function (options) {
5659 return ;
5760 }
5861
62+ observer . priority = options . priority ;
63+
5964 // If not, find all links and use IntersectionObserver.
6065 Array . from ( options . el . querySelectorAll ( 'a' ) , link => {
6166 observer . observe ( link ) ;
62-
63- // Generate loader functions for each link
64- const uri = link . href ;
65- loaderFunctions . set ( uri , ( ) => {
66- loaderFunctions . delete ( uri ) ;
67- prefetch ( uri , options . priority ) ;
68- } ) ;
67+ toPrefetch . add ( link . href ) ;
6968 } ) ;
7069 } , { timeout : options . timeout } ) ;
7170}
You can’t perform that action at this time.
0 commit comments