instantpage.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*! instant.page v0.0.0 - (C) 2019 Alexandre Dieulot - https://instant.page/license */
  2. let urlToPreload
  3. let mouseoverTimer
  4. const prefetcher = document.createElement('link')
  5. const isSupported = prefetcher.relList && prefetcher.relList.supports && prefetcher.relList.supports('prefetch')
  6. if (isSupported) {
  7. prefetcher.rel = 'prefetch'
  8. document.head.appendChild(prefetcher)
  9. document.addEventListener('mouseover', mouseoverListener, {
  10. capture: true,
  11. passive: true,
  12. })
  13. }
  14. function mouseoverListener(event) {
  15. const linkElement = event.target.closest('a')
  16. if (!linkElement) {
  17. return
  18. }
  19. if (!isPreloadable(linkElement)) {
  20. return
  21. }
  22. linkElement.addEventListener('mouseout', mouseoutListener)
  23. urlToPreload = linkElement.href
  24. mouseoverTimer = setTimeout(() => {
  25. preload(linkElement.href)
  26. mouseoverTimer = undefined
  27. }, 65)
  28. }
  29. function mouseoutListener(event) {
  30. if (event.relatedTarget && event.target.closest('a') == event.relatedTarget.closest('a')) {
  31. return
  32. }
  33. if (mouseoverTimer) {
  34. clearTimeout(mouseoverTimer)
  35. mouseoverTimer = undefined
  36. }
  37. else {
  38. urlToPreload = undefined
  39. stopPreloading()
  40. }
  41. }
  42. function isPreloadable(linkElement) {
  43. if (urlToPreload == linkElement.href) {
  44. return false
  45. }
  46. const urlObject = new URL(linkElement.href)
  47. if (urlObject.origin != location.origin) {
  48. return false
  49. }
  50. if (urlObject.pathname + urlObject.search == location.pathname + location.search && urlObject.hash) {
  51. return
  52. }
  53. if ('noInstant' in linkElement.dataset) {
  54. return false
  55. }
  56. return true
  57. }
  58. function preload(url) {
  59. prefetcher.href = url
  60. }
  61. function stopPreloading() {
  62. /* The spec says an empty string should abort the prefetching
  63. * but Firefox 64 interprets it as a relative URL to prefetch. */
  64. prefetcher.removeAttribute('href')
  65. }