8 – How can I disable all the JavaScript scripts?

I am trying to disable all the JavaScript on all the pages of a Drupal 8 site for anonymous users if there is a particular header in the request object.

I tried out this code.

/**
 * Implements hook_js_alter().
 */
function MODULE_js_alter(&$javascript, AttachedAssetsInterface $assets) {
  if (MODULE_get_headers()) {
    unset($javascript);
  }
}
/**
 * Implements hook_page_attachments_alter().
 */
function MODULE_page_attachments_alter(&$attachments) {
  // This is for disabling all 3rd party libraries. 
  if (MODULE_get_headers()) {
    $attachments('#attached')('library') = ();
  }
}

It does seem to work to a certain extent. It disables most of JavaScript, but I am seeing the styles are also off (which might be expected) as the CSS libraries might also be removed.

Is there a better way to do this? Maybe Drupal has an option to turn off all the JavaScript.