How to detect 404 in THEME file
Got it.

In your hook within your theme file:

  $status = \Drupal::requestStack()-&gt;getCurrentRequest()-&gt;attributes-&gt;get('exception');
  if ($status &amp;&amp; $status-&gt;getStatusCode() == 404) {
    $page['#attached']['library'][] = 'themename/notfound';
  }