Attach JavaScript and CSS assets when render entity using Views REST export Goal:


Have a REST endpoint which will deliver list of rendered entities.
Each row should contain a ready-to-use HTML.
All the necessary JavaScript and CSS files should be also included on the page.
P.S. I need this endpoint for lazy-loading of entities.


Given:


I have a Views display with REST export.
I render entities there using the rendered_entity field.


Problem:


JavaScript and CSS files, needed for rendering, are not attached on the page.
E.g. scripts for display of an image gallery, lazy-loading of images.
We came up with a new module: Ajax Assets Plus.

The module provides a new render which collects all the necessary libraries during rendering. It results in an array:

return [
      'commands' ['An array with ajax commands for settings'],
      'content' =&gt; 'Rendered content(HTML)',
      'libraries' =&gt; 'all the needed assets grouped into libraries',
    ];


We had to to override the AjaxResponseAttachmentsProcessor and AssetResolver classes.

Also we implemented the hook_js_settings_build() to collect all the libraries on the page, as ajaxPageState does not contain all the dependencies.