Skip to content

Commit e764879

Browse files
committed
Prefetch with high fetchPriority on mouse & touch events
By default, a prefetch is loaded with a low priority. When there’s a fair chance that this prefetch is going to be used in the near term (= after a touch/mouse event), giving it a high priority would help make the page load faster in case there are other resources loading. Prioritizing it implicitly means deprioritizing every other resource that’s loading on the page. Due to HTML documents usually being much smaller than other resources (notably images and JavaScript), this theft of bandwidth should rarely be detrimental. Closes #117
1 parent 982d845 commit e764879

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

instantpage.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function touchstartListener(event) {
116116
return
117117
}
118118

119-
preload(linkElement.href)
119+
preload(linkElement.href, 'high')
120120
}
121121

122122
function mouseoverListener(event) {
@@ -142,7 +142,7 @@ function mouseoverListener(event) {
142142
linkElement.addEventListener('mouseout', mouseoutListener, {passive: true})
143143

144144
mouseoverTimer = setTimeout(() => {
145-
preload(linkElement.href)
145+
preload(linkElement.href, 'high')
146146
mouseoverTimer = undefined
147147
}, delayOnHover)
148148
}
@@ -154,7 +154,7 @@ function mousedownListener(event) {
154154
return
155155
}
156156

157-
preload(linkElement.href)
157+
preload(linkElement.href, 'high')
158158
}
159159

160160
function mouseoutListener(event) {
@@ -231,14 +231,15 @@ function isPreloadable(linkElement) {
231231
return true
232232
}
233233

234-
function preload(url) {
234+
function preload(url, fetchPriority = 'auto') {
235235
if (prefetches.has(url)) {
236236
return
237237
}
238238

239239
const prefetcher = document.createElement('link')
240240
prefetcher.rel = 'prefetch'
241241
prefetcher.href = url
242+
prefetcher.fetchPriority = fetchPriority
242243
document.head.appendChild(prefetcher)
243244

244245
prefetches.add(url)

0 commit comments

Comments
 (0)