Pārlūkot izejas kodu

Don’t prefetch a link that’s already prefetched

Alexandre Dieulot 5 gadi atpakaļ
vecāks
revīzija
ba6a1736e1
1 mainītis faili ar 7 papildinājumiem un 1 dzēšanām
  1. 7 1
      instantpage.js

+ 7 - 1
instantpage.js

@@ -2,7 +2,7 @@
 
 let mouseoverTimer
 let lastTouchTimestamp
-
+const prefetches = new Set()
 const prefetchElement = document.createElement('link')
 const isSupported = prefetchElement.relList && prefetchElement.relList.supports && prefetchElement.relList.supports('prefetch')
                     && window.IntersectionObserver && 'isIntersecting' in IntersectionObserverEntry.prototype
@@ -185,8 +185,14 @@ function isPreloadable(linkElement) {
 }
 
 function preload(url) {
+  if (prefetches.has(url)) {
+    return
+  }
+
   const prefetcher = document.createElement('link')
   prefetcher.rel = 'prefetch'
   prefetcher.href = url
   document.head.appendChild(prefetcher)
+
+  prefetches.add(url)
 }