I am developing a custom module in Drupal 8. My custom module include many JavaScript and CSS files which make the site really slow and I don't want them in all pages. I have developed some custom blocks and each JavaScript and CSS library correspond to a block. I want those files on those pages of the site where my custom blocks are visible.
In my last Drupal 8 project, I have done something like the following.
function HOOK_page_attachments(array &$attachments) {
$user = \Drupal::currentUser()->getRoles();
if(in_array("administrator", $user)){
$attachments['#attached']['library'][] = 'module/library';
}
}
I am not sure how to check which blocks are visible in the page.