Browse Source

Don’t prefetch a link that’s already prefetched

Alexandre Dieulot 5 years ago
parent
commit
ba6a1736e1
1 changed files with 7 additions and 1 deletions
  1. 7 1
      instantpage.js

+ 7 - 1
instantpage.js

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