@@ -132,38 +132,24 @@ export default class SwupPreloadPlugin extends Plugin {
132
132
swup . preload = this . preload ;
133
133
swup . preloadLinks = this . preloadLinks ;
134
134
135
- // Register handlers for preloading on attention: mouseenter, touchstart, focus
136
- const { linkSelector : selector } = swup . options ;
137
- const opts = { passive : true , capture : true } ;
138
- this . mouseEnterDelegate = swup . delegateEvent (
139
- selector ,
140
- 'mouseenter' ,
141
- this . onMouseEnter ,
142
- opts
143
- ) ;
144
- this . touchStartDelegate = swup . delegateEvent (
145
- selector ,
146
- 'touchstart' ,
147
- this . onTouchStart ,
148
- opts
149
- ) ;
150
- this . focusDelegate = swup . delegateEvent ( selector , 'focus' , this . onFocus , opts ) ;
151
-
152
135
// Inject custom promise whenever a page is loaded
153
136
this . replace ( 'page:load' , this . onPageLoad ) ;
154
137
155
138
// Preload links with [data-swup-preload] attr
156
- if ( this . options . preloadHoveredLinks ) {
157
- this . preloadLinks ( ) ;
158
- this . on ( 'page:view' , ( ) => this . preloadLinks ( ) ) ;
159
- }
139
+ this . preloadLinks ( ) ;
140
+ this . on ( 'page:view' , ( ) => this . preloadLinks ( ) ) ;
160
141
161
142
// Preload visible links in viewport
162
143
if ( this . options . preloadVisibleLinks . enabled ) {
163
144
this . preloadVisibleLinks ( ) ;
164
145
this . on ( 'page:view' , ( ) => this . preloadVisibleLinks ( ) ) ;
165
146
}
166
147
148
+ // Preload links on attention
149
+ if ( this . options . preloadHoveredLinks ) {
150
+ this . preloadLinksOnAttention ( ) ;
151
+ }
152
+
167
153
// Cache unmodified DOM of initial/current page
168
154
if ( this . options . preloadInitialPage ) {
169
155
this . preload ( getCurrentUrl ( ) ) ;
@@ -323,6 +309,32 @@ export default class SwupPreloadPlugin extends Plugin {
323
309
} ) ;
324
310
}
325
311
312
+ /**
313
+ * Register handlers for preloading on attention:
314
+ * - mouseenter
315
+ * - touchstart
316
+ * - focus
317
+ */
318
+ protected preloadLinksOnAttention ( ) {
319
+ const { swup } = this ;
320
+
321
+ const { linkSelector : selector } = swup . options ;
322
+ const opts = { passive : true , capture : true } ;
323
+ this . mouseEnterDelegate = swup . delegateEvent (
324
+ selector ,
325
+ 'mouseenter' ,
326
+ this . onMouseEnter ,
327
+ opts
328
+ ) ;
329
+ this . touchStartDelegate = swup . delegateEvent (
330
+ selector ,
331
+ 'touchstart' ,
332
+ this . onTouchStart ,
333
+ opts
334
+ ) ;
335
+ this . focusDelegate = swup . delegateEvent ( selector , 'focus' , this . onFocus , opts ) ;
336
+ }
337
+
326
338
/**
327
339
* Start observing links in the viewport for preloading.
328
340
* Calling this repeatedly re-checks for links after DOM updates.
0 commit comments