instantpage.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*! instant.page v1.0.0 - (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. const allowQueryString = 'instantAllowQueryString' in document.body.dataset
  8. if (isSupported) {
  9. prefetcher.rel = 'prefetch'
  10. document.head.appendChild(prefetcher)
  11. const eventListenersOptions = {
  12. capture: true,
  13. passive: true,
  14. }
  15. document.addEventListener('touchstart', touchstartListener, eventListenersOptions)
  16. document.addEventListener('mouseover', mouseoverListener, eventListenersOptions)
  17. }
  18. function touchstartListener(event) {
  19. /* Chrome on Android calls mouseover before touchcancel so `lastTouchTimestamp`
  20. * must be assigned on touchstart to be measured on mouseover. */
  21. lastTouchTimestamp = performance.now()
  22. const linkElement = event.target.closest('a')
  23. if (!linkElement) {
  24. return
  25. }
  26. if (!isPreloadable(linkElement)) {
  27. return
  28. }
  29. linkElement.addEventListener('touchcancel', touchendAndTouchcancelListener, {passive: true})
  30. linkElement.addEventListener('touchend', touchendAndTouchcancelListener, {passive: true})
  31. urlToPreload = linkElement.href
  32. preload(linkElement.href)
  33. }
  34. function touchendAndTouchcancelListener() {
  35. urlToPreload = undefined
  36. stopPreloading()
  37. }
  38. function mouseoverListener(event) {
  39. if (performance.now() - lastTouchTimestamp < 1100) {
  40. return
  41. }
  42. const linkElement = event.target.closest('a')
  43. if (!linkElement) {
  44. return
  45. }
  46. if (!isPreloadable(linkElement)) {
  47. return
  48. }
  49. linkElement.addEventListener('mouseout', mouseoutListener, {passive: true})
  50. urlToPreload = linkElement.href
  51. mouseoverTimer = setTimeout(() => {
  52. preload(linkElement.href)
  53. mouseoverTimer = undefined
  54. }, 65)
  55. }
  56. function mouseoutListener(event) {
  57. if (event.relatedTarget && event.target.closest('a') == event.relatedTarget.closest('a')) {
  58. return
  59. }
  60. if (mouseoverTimer) {
  61. clearTimeout(mouseoverTimer)
  62. mouseoverTimer = undefined
  63. }
  64. else {
  65. urlToPreload = undefined
  66. stopPreloading()
  67. }
  68. }
  69. function isPreloadable(linkElement) {
  70. if (urlToPreload == linkElement.href) {
  71. return
  72. }
  73. const urlObject = new URL(linkElement.href)
  74. if (urlObject.origin != location.origin) {
  75. return
  76. }
  77. if (!allowQueryString && urlObject.search) {
  78. return
  79. }
  80. if (urlObject.pathname + urlObject.search == location.pathname + location.search && urlObject.hash) {
  81. return
  82. }
  83. if ('noInstant' in linkElement.dataset) {
  84. return
  85. }
  86. return true
  87. }
  88. function preload(url) {
  89. prefetcher.href = url
  90. }
  91. function stopPreloading() {
  92. /* The spec says an empty string should abort the prefetching
  93. * but Firefox 64 interprets it as a relative URL to prefetch. */
  94. prefetcher.removeAttribute('href')
  95. }