instantpage.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*! instant.page v2.0.1 - (C) 2019 Alexandre Dieulot - https://instant.page/license */
  2. let mouseoverTimer
  3. let lastTouchTimestamp
  4. const prefetchElement = document.createElement('link')
  5. const isSupported = prefetchElement.relList && prefetchElement.relList.supports && prefetchElement.relList.supports('prefetch')
  6. && window.IntersectionObserver && 'isIntersecting' in IntersectionObserverEntry.prototype
  7. const isDataSaverEnabled = navigator.connection && navigator.connection.saveData
  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. useViewport = true
  29. }
  30. }
  31. else {
  32. const milliseconds = parseInt(intensity)
  33. if (!isNaN(milliseconds)) {
  34. delayOnHover = milliseconds
  35. }
  36. }
  37. }
  38. if (isSupported && !isDataSaverEnabled) {
  39. const eventListenersOptions = {
  40. capture: true,
  41. passive: true,
  42. }
  43. if (!useMousedownOnly) {
  44. document.addEventListener('touchstart', touchstartListener, eventListenersOptions)
  45. }
  46. if (!useMousedown) {
  47. document.addEventListener('mouseover', mouseoverListener, eventListenersOptions)
  48. }
  49. else {
  50. document.addEventListener('mousedown', mousedownListener, eventListenersOptions)
  51. }
  52. if (useViewport) {
  53. let triggeringFunction
  54. if (window.requestIdleCallback) {
  55. triggeringFunction = (callback) => {
  56. requestIdleCallback(callback, {
  57. timeout: 1500,
  58. })
  59. }
  60. }
  61. else {
  62. triggeringFunction = (callback) => {
  63. callback()
  64. }
  65. }
  66. triggeringFunction(() => {
  67. const intersectionObserver = new IntersectionObserver((entries) => {
  68. entries.forEach((entry) => {
  69. if (entry.isIntersecting) {
  70. const linkElement = entry.target
  71. intersectionObserver.unobserve(linkElement)
  72. preload(linkElement.href)
  73. }
  74. })
  75. })
  76. document.querySelectorAll('a').forEach((linkElement) => {
  77. if (isPreloadable(linkElement)) {
  78. intersectionObserver.observe(linkElement)
  79. }
  80. })
  81. })
  82. }
  83. }
  84. function touchstartListener(event) {
  85. /* Chrome on Android calls mouseover before touchcancel so `lastTouchTimestamp`
  86. * must be assigned on touchstart to be measured on mouseover. */
  87. lastTouchTimestamp = performance.now()
  88. const linkElement = event.target.closest('a')
  89. if (!isPreloadable(linkElement)) {
  90. return
  91. }
  92. preload(linkElement.href)
  93. }
  94. function mouseoverListener(event) {
  95. if (performance.now() - lastTouchTimestamp < 1100) {
  96. return
  97. }
  98. const linkElement = event.target.closest('a')
  99. if (!isPreloadable(linkElement)) {
  100. return
  101. }
  102. linkElement.addEventListener('mouseout', mouseoutListener, {passive: true})
  103. mouseoverTimer = setTimeout(() => {
  104. preload(linkElement.href)
  105. mouseoverTimer = undefined
  106. }, delayOnHover)
  107. }
  108. function mousedownListener(event) {
  109. const linkElement = event.target.closest('a')
  110. if (!isPreloadable(linkElement)) {
  111. return
  112. }
  113. linkElement.addEventListener('mouseout', mouseoutListener, {passive: true})
  114. preload(linkElement.href)
  115. }
  116. function mouseoutListener(event) {
  117. if (event.relatedTarget && event.target.closest('a') == event.relatedTarget.closest('a')) {
  118. return
  119. }
  120. if (mouseoverTimer) {
  121. clearTimeout(mouseoverTimer)
  122. mouseoverTimer = undefined
  123. }
  124. }
  125. function isPreloadable(linkElement) {
  126. if (!linkElement || !linkElement.href) {
  127. return
  128. }
  129. if (useWhitelist && !('instant' in linkElement.dataset)) {
  130. return
  131. }
  132. if (!allowExternalLinks && linkElement.origin != location.origin && !('instant' in linkElement.dataset)) {
  133. return
  134. }
  135. if (!['http:', 'https:'].includes(linkElement.protocol)) {
  136. return
  137. }
  138. if (linkElement.protocol == 'http:' && location.protocol == 'https:') {
  139. return
  140. }
  141. if (!allowQueryString && linkElement.search && !('instant' in linkElement.dataset)) {
  142. return
  143. }
  144. if (linkElement.hash && linkElement.pathname + linkElement.search == location.pathname + location.search) {
  145. return
  146. }
  147. if ('noInstant' in linkElement.dataset) {
  148. return
  149. }
  150. return true
  151. }
  152. function preload(url) {
  153. const prefetcher = document.createElement('link')
  154. prefetcher.rel = 'prefetch'
  155. prefetcher.href = url
  156. document.head.appendChild(prefetcher)
  157. }