-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent references to nodes swapped out of the dom accumulating as detached elements in memory #2091
Conversation
… detached nodes (memory leak).
Thank you @croxton! |
@croxton @1cg |
@grey4owl Looking at your extension it should be possible to store the element attributes you need before the swap happens on the |
@croxton It's not a problem to get attributes from target element that initiate request, it's a problem to get new target that is gonna be swapped, and with an old approach I didn't even have to do that because I could simply push those attributes that I want to preserve in |
@croxton And yes I know that you can get new target after swapping and you can set new attributes but then you get alpinejs problems.. So I need a way to set custom attributes to new element that is gonna be swapped but before request is finished so that response be new element with those new attributes.. |
On onEvent : function(name, event) {
if (name === "htmx:beforeSwap") {
let incomingDOM = new DOMParser().parseFromString(event.detail.xhr.response, "text/html");
// Do stuff with incoming dom, such as get attributes from an element and cache them
// This should work to modify the response before it is swapped, if you need to:
event.detail.xhr.response = incomingDOM.body.innerHTML;
}
} |
@croxton I mange to make it work like you explained but i used |
Great! Htmx's events are designed to allow listeners to mutate the objects passed to them, so I don't think it's wrong. You're extending htmx, exactly as intended. |
Description
I noticed that detached nodes accumulated in a project after multiple swaps. Memory heap snapshots contained detached elements with references to
htmx-internal-data
and areplacedWith
property caused by this line:Ref:
htmx/src/htmx.js
Line 990 in 206912b
Important notes:
replacedWith
property, there are no other references to it in the source code.htmx-internal-data
incleanUpElement()
because some elements would retain content in this structure (with references to elements no longer in the dom) despite the preceding cleanup - I did not investigate why, or determine if there is ever a reason to retain data inhtmx-internal-data
when an element is "cleaned up".Testing
Observed manually using Chrome's developer tools to take heap snapshots while performing a sequence of typical user tasks on a complex htmx application. And checked with Microsoft Edge which has a useful 'Detached Elements' feature. These changes prevented detached nodes accumulating.
Passes htmx automated tests.
Checklist
master
for website changes,dev
forsource changes)
approved via an issue
npm run test
) and verified that it succeeded