Drupal - Loading a library only for the front page
function mytheme_preprocess_page(&$variables) {
if (\Drupal::routeMatch()->getRouteName() == 'view.frontpage.page_1') {
$variables['#attached']['library'][] = 'mytheme/cuddly-slider';
}
}
The other workaround would be using the method from drupal's path matcher interface which returns true if the current page is the front page.
function themename_preprocess_page(&$variables) {
//using the method from the pathmatcherinterface
if (\Drupal::service('path.matcher')->isFrontPage()) {
$variables['#attached']['library'][] = 'themename/cuddly-slider';
}
}