instantpage.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 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. const eventListenersOptions = {
  41. capture: true,
  42. passive: true,
  43. }
  44. if (!useMousedownOnly) {
  45. document.addEventListener('touchstart', touchstartListener, eventListenersOptions)
  46. }
  47. if (!useMousedown) {
  48. document.addEventListener('mouseover', mouseoverListener, eventListenersOptions)
  49. }
  50. else {
  51. document.addEventListener('mousedown', mousedownListener, eventListenersOptions)
  52. }
  53. if (useViewport) {
  54. let triggeringFunction
  55. if (window.requestIdleCallback) {
  56. triggeringFunction = (callback) => {
  57. requestIdleCallback(callback, {
  58. timeout: 1500,
  59. })
  60. }
  61. }
  62. else {
  63. triggeringFunction = (callback) => {
  64. callback()
  65. }
  66. }
  67. triggeringFunction(() => {
  68. const intersectionObserver = new IntersectionObserver((entries) => {
  69. entries.forEach((entry) => {
  70. if (entry.isIntersecting) {
  71. const linkElement = entry.target
  72. intersectionObserver.unobserve(linkElement)
  73. preload(linkElement.href)
  74. }
  75. })
  76. })
  77. document.querySelectorAll('a').forEach((linkElement) => {
  78. if (isPreloadable(linkElement)) {
  79. intersectionObserver.observe(linkElement)
  80. }
  81. })
  82. })
  83. }
  84. }
  85. function touchstartListener(event) {
  86. /* Chrome on Android calls mouseover before touchcancel so `lastTouchTimestamp`
  87. * must be assigned on touchstart to be measured on mouseover. */
  88. lastTouchTimestamp = performance.now()
  89. const linkElement = event.target.closest('a')
  90. if (!isPreloadable(linkElement)) {
  91. return
  92. }
  93. preload(linkElement.href)
  94. }
  95. function mouseoverListener(event) {
  96. if (performance.now() - lastTouchTimestamp < 1100) {
  97. return
  98. }
  99. const linkElement = event.target.closest('a')
  100. if (!isPreloadable(linkElement)) {
  101. return
  102. }
  103. linkElement.addEventListener('mouseout', mouseoutListener, {passive: true})
  104. mouseoverTimer = setTimeout(() => {
  105. preload(linkElement.href)
  106. mouseoverTimer = undefined
  107. }, delayOnHover)
  108. }
  109. function mousedownListener(event) {
  110. const linkElement = event.target.closest('a')
  111. if (!isPreloadable(linkElement)) {
  112. return
  113. }
  114. linkElement.addEventListener('mouseout', mouseoutListener, {passive: true})
  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. }