CDN JS -- cannot seem to weight it I'm adding external JS in my Drupal 8 site (//cdn.leafletjs.com/leaflet-0.7.2/leaflet.js) and I cannot seem to weight it using hook_js_alter. I can weight it if I throw it in a themename.libraries.yml file but then it gets rendered in every page. Ideally I want to only render the script in the home page. So this works:

function MYTHEME_js_alter(&amp;$js) {
  if (drupal_is_front_page()) {
    $leaflet = '//cdn.leafletjs.com/leaflet-0.7.2/leaflet.js';
    $js[$leaflet] = drupal_js_defaults();
    $js[$leaflet]['data'] = $leaflet;
    $js[$leaflet]['type'] = 'external';
    $js[$leaflet]['every_page'] = TRUE;
    $js[$leaflet]['weight'] = -100;
  }
}


... but it weights it last and I'd like to weight it so it loads much earlier. 

if I load it via a theme.libaries.yml file, it weights it lower but it renders on every page.

MYTHEME-corescripts:
  version: VERSION
  js:
    //cdn.leafletjs.com/leaflet-0.7.2/leaflet.js: {}
    js/scripts.js: {}


I'm open to this method if I could somehow add context to the YAML file to indicate only fore the home page.
The method below works superbly. hook_page_alter is the way to go and the #attached method. (as suggested in a comment instead of hook_preprocess_page I didn't know this would work for external js but it does. Pretty cool. With this method and a nested array with weight, it works as expected. 

function MYTHEME_page_alter($cache) {

  if (drupal_is_front_page()) {
    // Render leaflet only on the home page
    $leaflet = array(
      '#attached' =&gt; array(
        'js' =&gt; array(
          '//cdn.leafletjs.com/leaflet-0.7.2/leaflet.js' =&gt; array(
            'group' =&gt; JS_LIBRARY,
            'weight' =&gt; -999
          ),
        ),
      ),
    );
    \Drupal::service('renderer')-&gt;renderRoot($leaflet);
  }
}


Also of note is that we use JS_LIBRARY instead of JS_THEME here.