instantpage.js 5.3 KB

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