Explorar el Código

Don’t prefetch a link that’s already prefetched

Alexandre Dieulot hace 5 años
padre
commit
ba6a1736e1
Se han modificado 1 ficheros con 7 adiciones y 1 borrados
  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)
 }