htmx icon indicating copy to clipboard operation
htmx copied to clipboard

Preload extension fails with Safari on macOS

Open michrome opened this issue 1 year ago • 2 comments
trafficstars

Summary

The preload extension fails to initialise on Safari, with the error TypeError: null is not an object (evaluating 'event.target.querySelectorAll') - preload.js:138.

Line 138 in [preload.js](https://github.com/bigskysoftware/htmx/blob/master/src/ext/preload.js) is:

// Search for all child nodes that have a "preload" attribute
event.target.querySelectorAll("[preload]").forEach(function(node) {

    // Initialize the node with the "preload" attribute
    init(node)

    // Initialize all child elements that are anchors or have `hx-get` (use with care)
    node.querySelectorAll("a,[hx-get],[data-hx-get]").forEach(init)
})

It appears Safari returns null for event.target when other browsers do not.

Versions

  • Safari v17.4 on macOS Sonoma
  • HTMX v1.9.12
  • Preload extension v1.9.12

Example for reproduction

<!doctype html>
<html lang="en-GB">
    <head>
        <script src="https://unpkg.com/[email protected]"></script>
        <script src="https://unpkg.com/[email protected]/dist/ext/preload.js"></script>
    </head>
    <body hx-ext="preload">
        <ul id="list">
            <li>Test</li>
        </ul>
        <div hx-get="/some_url" hx-target="#list" hx-select="#list" preload>
            Get Some HTML
        </div>
    </body>
</html>

Screenshot 2024-04-24 at 21 35 04

michrome avatar Apr 24 '24 20:04 michrome

Would replacing event.target.querySelectorAll("[preload]").forEach(function(node) { with document.querySelectorAll("[preload]").forEach(function(node) { resolve this issue?

michrome avatar Apr 24 '24 21:04 michrome

I wonder if the same fix than in #2391 could be applied here ? Seems like the same underlying issue

Telroshan avatar Apr 25 '24 08:04 Telroshan