instantpage.js 5.8 KB

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