I have built a sort of component framework which attempts to solve the problem of scripts needing a lifecycle (mount / unmount / update) so that memory is used efficiently, when using htmx as a SPA. I'm not sure how generalisable it is but I'll probably make it a repo. Maybe you can take some ideas from it.