Skip to content

Commit 48cd7dc

Browse files
authored
Merge pull request #114 from swup/fix/preload-hovered-links
Fix `options.preloadHoveredLinks`
2 parents 095dd5d + 04c4f96 commit 48cd7dc

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## [3.2.7] - 2023-10-20
4+
5+
- Fix `options.preloadHoveredLinks`
6+
37
## [3.2.6] - 2023-10-13
48

59
- Fix `swup.preload` function signature
@@ -107,6 +111,7 @@
107111

108112
- Initial release
109113

114+
[3.2.7]: https://github.com/swup/preload-plugin/releases/tag/3.2.7
110115
[3.2.6]: https://github.com/swup/preload-plugin/releases/tag/3.2.6
111116
[3.2.5]: https://github.com/swup/preload-plugin/releases/tag/3.2.5
112117
[3.2.4]: https://github.com/swup/preload-plugin/releases/tag/3.2.4

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@swup/preload-plugin",
33
"amdName": "SwupPreloadPlugin",
4-
"version": "3.2.6",
4+
"version": "3.2.7",
55
"description": "A swup plugin for preloading pages and faster navigation",
66
"type": "module",
77
"source": "src/index.ts",

src/index.ts

+33-21
Original file line numberDiff line numberDiff line change
@@ -132,38 +132,24 @@ export default class SwupPreloadPlugin extends Plugin {
132132
swup.preload = this.preload;
133133
swup.preloadLinks = this.preloadLinks;
134134

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-
152135
// Inject custom promise whenever a page is loaded
153136
this.replace('page:load', this.onPageLoad);
154137

155138
// 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());
160141

161142
// Preload visible links in viewport
162143
if (this.options.preloadVisibleLinks.enabled) {
163144
this.preloadVisibleLinks();
164145
this.on('page:view', () => this.preloadVisibleLinks());
165146
}
166147

148+
// Preload links on attention
149+
if (this.options.preloadHoveredLinks) {
150+
this.preloadLinksOnAttention();
151+
}
152+
167153
// Cache unmodified DOM of initial/current page
168154
if (this.options.preloadInitialPage) {
169155
this.preload(getCurrentUrl());
@@ -323,6 +309,32 @@ export default class SwupPreloadPlugin extends Plugin {
323309
});
324310
}
325311

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+
326338
/**
327339
* Start observing links in the viewport for preloading.
328340
* Calling this repeatedly re-checks for links after DOM updates.

0 commit comments

Comments
 (0)