It was quite some work to adapt the rendering process. There is a lot of code, I will just summarise the solution. If you want more details, just write a comment.
The solution looks similar to my ideas/hypothesis in the question's description.
I created a custom render which collects all the libraries during rendering in an array. It results in an array:
return [ 'commands' ['An array with ajax commands for settings'], 'content' => 'Rendered content(HTML)', 'libraries' => 'all the needed assets grouped into libraries', ];
I had to to override the
Also I implemented the
hook_js_settings_build() to collect all the libraries on the page, as
ajaxPageState does not contain all the dependencies.
At the front-end side it is not possible to attach all the assets by comparing
ajaxPageState and the response from a Web Socket.