preload.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // This adds the "preload" extension to htmx. By default, this will
  2. // preload the targets of any tags with `href` or `hx-get` attributes
  3. // if they also have a `preload` attribute as well. See documentation
  4. // for more detauls
  5. htmx.defineExtension("preload", {
  6. onEvent: function(name, event) {
  7. // Only take actions on "htmx:afterProcessNode"
  8. if (name !== "htmx:afterProcessNode") {
  9. return;
  10. }
  11. // SOME HELPER FUNCTIONS WE'LL NEED ALONG THE WAY
  12. // attr gets the closest non-empty value from the attribute.
  13. var attr = function(node, property) {
  14. if (node == undefined) {return undefined;}
  15. return node.getAttribute(property) || node.getAttribute("data-" + property) || attr(node.parentElement, property)
  16. }
  17. // load handles the actual HTTP fetch, and uses htmx.ajax in cases where we're
  18. // preloading an htmx resource (this sends the same HTTP headers as a regular htmx request)
  19. var load = function(node) {
  20. // Called after a successful AJAX request, to mark the
  21. // content as loaded (and prevent additional AJAX calls.)
  22. var done = function(html) {
  23. node.preloadState = "DONE"
  24. if (attr(node, "preload-images") == "true") {
  25. document.createElement("div").innerHTML = html // create and populate a node to load linked resources, too.
  26. }
  27. }
  28. return function() {
  29. // If this value has already been loaded, then do not try again.
  30. if (node.preloadState !== "READY") {
  31. return;
  32. }
  33. // Special handling for HX-GET - use built-in htmx.ajax function
  34. // so that headers match other htmx requests, then set
  35. // node.preloadState = TRUE so that requests are not duplicated
  36. // in the future
  37. var hxGet = node.getAttribute("hx-get") || node.getAttribute("data-hx-get")
  38. if (hxGet) {
  39. htmx.ajax("GET", hxGet, {handler:function(elt, info) {
  40. done(info.xhr.responseText);
  41. }});
  42. return;
  43. }
  44. // Otherwise, perform a standard xhr request, then set
  45. // node.preloadState = TRUE so that requests are not duplicated
  46. // in the future.
  47. if (node.getAttribute("href")) {
  48. var r = new XMLHttpRequest();
  49. r.open("GET", node.getAttribute("href"));
  50. r.onload = function() {done(r.responseText);};
  51. r.send();
  52. return;
  53. }
  54. }
  55. }
  56. // This function processes a specific node and sets up event handlers.
  57. // We'll search for nodes and use it below.
  58. var init = function(node) {
  59. // If this node DOES NOT include a "GET" transaction, then there's nothing to do here.
  60. if (node.getAttribute("href") + node.getAttribute("hx-get") + node.getAttribute("data-hx-get") == "") {
  61. return;
  62. }
  63. // Guarantee that we only initialize each node once.
  64. if (node.preloadState !== undefined) {
  65. return;
  66. }
  67. // Get event name from config.
  68. var on = attr(node, "preload") || "mousedown"
  69. // FALL THROUGH to here means we need to add an EventListener
  70. // Apply the listener to the node
  71. node.addEventListener(on, function(evt) {
  72. if (node.preloadState === "PAUSE") { // Only add one event listener
  73. node.preloadState = "READY"; // Requred for the `load` function to trigger
  74. // Special handling for "mouseover" events. Wait 100ms before triggering load.
  75. if (on === "mouseover") {
  76. window.setTimeout(load(node), 100);
  77. } else {
  78. load(node)() // all other events trigger immediately.
  79. }
  80. }
  81. })
  82. // Special handling for certain built-in event handlers
  83. switch (on) {
  84. case "mouseover":
  85. // Mirror `touchstart` events (fires immediately)
  86. node.addEventListener("touchstart", load(node));
  87. // WHhen the mouse leaves, immediately disable the preload
  88. node.addEventListener("mouseout", function(evt) {
  89. if ((evt.target === node) && (node.preloadState === "READY")) {
  90. node.preloadState = "PAUSE";
  91. }
  92. })
  93. break;
  94. case "mousedown":
  95. // Mirror `touchstart` events (fires immediately)
  96. node.addEventListener("touchstart", load(node));
  97. break;
  98. }
  99. // Mark the node as ready to run.
  100. node.preloadState = "PAUSE";
  101. htmx.trigger(node, "preload:init") // This event can be used to load content immediately.
  102. }
  103. // Search for all child nodes that have a "preload" attribute
  104. event.target.querySelectorAll("[preload]").forEach(function(node) {
  105. // Initialize the node with the "preload" attribute
  106. init(node)
  107. // Initialize all child elements that are anchors or have `hx-get` (use with care)
  108. node.querySelectorAll("a,[hx-get],[data-hx-get").forEach(init)
  109. })
  110. }
  111. })