instantpage.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*! instant.page v2.0.1 - (C) 2019 Alexandre Dieulot - https://instant.page/license */
  2. let mouseoverTimer
  3. let lastTouchTimestamp
  4. const prefetches = new Set()
  5. const prefetchElement = document.createElement('link')
  6. const isSupported = prefetchElement.relList && prefetchElement.relList.supports && prefetchElement.relList.supports('prefetch')
  7. && window.IntersectionObserver && 'isIntersecting' in IntersectionObserverEntry.prototype
  8. const allowQueryString = 'instantAllowQueryString' in document.body.dataset
  9. const allowExternalLinks = 'instantAllowExternalLinks' in document.body.dataset
  10. const useWhitelist = 'instantWhitelist' in document.body.dataset
  11. let delayOnHover = 65
  12. let useMousedown = false
  13. let useMousedownOnly = false
  14. let useViewport = false
  15. if ('instantIntensity' in document.body.dataset) {
  16. const intensity = document.body.dataset.instantIntensity
  17. if (intensity.substr(0, 'mousedown'.length) == 'mousedown') {
  18. useMousedown = true
  19. if (intensity == 'mousedown-only') {
  20. useMousedownOnly = true
  21. }
  22. }
  23. else if (intensity == 'viewport') {
  24. /* Biggest iPhone resolution (which we want): 414 × 896 = 370944
  25. * Small 7" tablet resolution (which we don’t want): 600 × 1024 = 614400
  26. * Note that the viewport (which we check here) is smaller than the resolution due to the UI’s chrome */
  27. if (document.documentElement.clientWidth * document.documentElement.clientHeight < 450000) {
  28. if (!(navigator.connection && (navigator.connection.saveData || navigator.connection.effectiveType.includes('2g')))) {
  29. useViewport = true
  30. }
  31. }
  32. }
  33. else {
  34. const milliseconds = parseInt(intensity)
  35. if (!isNaN(milliseconds)) {
  36. delayOnHover = milliseconds
  37. }
  38. }
  39. }
  40. if (isSupported) {
  41. const eventListenersOptions = {
  42. capture: true,
  43. passive: true,
  44. }
  45. if (!useMousedownOnly) {
  46. document.addEventListener('touchstart', touchstartListener, eventListenersOptions)
  47. }
  48. if (!useMousedown) {
  49. document.addEventListener('mouseover', mouseoverListener, eventListenersOptions)
  50. }
  51. else {
  52. document.addEventListener('mousedown', mousedownListener, eventListenersOptions)
  53. }
  54. if (useViewport) {
  55. let triggeringFunction
  56. if (window.requestIdleCallback) {
  57. triggeringFunction = (callback) => {
  58. requestIdleCallback(callback, {
  59. timeout: 1500,
  60. })
  61. }
  62. }
  63. else {
  64. triggeringFunction = (callback) => {
  65. callback()
  66. }
  67. }
  68. triggeringFunction(() => {
  69. const intersectionObserver = new IntersectionObserver((entries) => {
  70. entries.forEach((entry) => {
  71. if (entry.isIntersecting) {
  72. const linkElement = entry.target
  73. intersectionObserver.unobserve(linkElement)
  74. preload(linkElement.href)
  75. }
  76. })
  77. })
  78. document.querySelectorAll('a').forEach((linkElement) => {
  79. if (isPreloadable(linkElement)) {
  80. intersectionObserver.observe(linkElement)
  81. }
  82. })
  83. })
  84. }
  85. }
  86. function touchstartListener(event) {
  87. /* Chrome on Android calls mouseover before touchcancel so `lastTouchTimestamp`
  88. * must be assigned on touchstart to be measured on mouseover. */
  89. lastTouchTimestamp = performance.now()
  90. const linkElement = event.target.closest('a')
  91. if (!isPreloadable(linkElement)) {
  92. return
  93. }
  94. preload(linkElement.href)
  95. }
  96. function mouseoverListener(event) {
  97. if (performance.now() - lastTouchTimestamp < 1100) {
  98. return
  99. }
  100. const linkElement = event.target.closest('a')
  101. if (!isPreloadable(linkElement)) {
  102. return
  103. }
  104. linkElement.addEventListener('mouseout', mouseoutListener, {passive: true})
  105. mouseoverTimer = setTimeout(() => {
  106. preload(linkElement.href)
  107. mouseoverTimer = undefined
  108. }, delayOnHover)
  109. }
  110. function mousedownListener(event) {
  111. const linkElement = event.target.closest('a')
  112. if (!isPreloadable(linkElement)) {
  113. return
  114. }
  115. preload(linkElement.href)
  116. }
  117. function mouseoutListener(event) {
  118. if (event.relatedTarget && event.target.closest('a') == event.relatedTarget.closest('a')) {
  119. return
  120. }
  121. if (mouseoverTimer) {
  122. clearTimeout(mouseoverTimer)
  123. mouseoverTimer = undefined
  124. }
  125. }
  126. function isPreloadable(linkElement) {
  127. if (!linkElement || !linkElement.href) {
  128. return
  129. }
  130. if (useWhitelist && !('instant' in linkElement.dataset)) {
  131. return
  132. }
  133. if (!allowExternalLinks && linkElement.origin != location.origin && !('instant' in linkElement.dataset)) {
  134. return
  135. }
  136. if (!['http:', 'https:'].includes(linkElement.protocol)) {
  137. return
  138. }
  139. if (linkElement.protocol == 'http:' && location.protocol == 'https:') {
  140. return
  141. }
  142. if (!allowQueryString && linkElement.search && !('instant' in linkElement.dataset)) {
  143. return
  144. }
  145. if (linkElement.hash && linkElement.pathname + linkElement.search == location.pathname + location.search) {
  146. return
  147. }
  148. if ('noInstant' in linkElement.dataset) {
  149. return
  150. }
  151. return true
  152. }
  153. function preload(url) {
  154. if (prefetches.has(url)) {
  155. return
  156. }
  157. const prefetcher = document.createElement('link')
  158. prefetcher.rel = 'prefetch'
  159. prefetcher.href = url
  160. document.head.appendChild(prefetcher)
  161. prefetches.add(url)
  162. }