instantpage.js 3.1 KB

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