jsDelivr CDN Libraries

Component ID

2338617

Component name

jsDelivr CDN Libraries

Component type

module

Maintenance status

Development status

Component security advisory coverage

covered

Downloads

4078

Component created

Component changed

Component body

Load libraries from the jsDelivr CDN. jsDelivr is a CDN service that load open source web assets from multiple CDN from over 80 locations. A community contribution from http://www.sooperthemes.com/#-Drupal-Themes..

I created this module because I think it's a great alternative to loading local libraries and jsDelivr has some cool features, like fuzzy version specification and loading multiple libraries in one request. Patches for improvement are welcome.

Usage with GUI:

Enable module, it has no dependencies. You can enter JS and CSS libraries in the configuration form at Configuration > Web Services > jsDelivr.

Usage with API functions:

You may also add libraries programmatically by using the utility functions jsdelivr_detect and js_delivr_add.
It's important to understand the for JS libraries we use the /g/ API, which supports version aliases and partial version specification (e.g. latest 3.x or latest 1.2.x). For CSS we only support adding complete file paths, because while using the /g/ API you loose any assets that are specified in the library using relative paths.

Example:

module_load_include('module', 'jsdelivr');
if (function_exists('jsdelivr_detect') && (jsdelivr_detect('flexslider') == FALSE))) {
  jsdelivr_add_js('flexslider@2');
}

The above code checks if there is any occurence of Flexslider in the library list and if there is not it adds the latest Flexslider 2.x version.
For more info about jsDelivr version aliases see: https://github.com/jsdelivr/jsdelivr#version-aliasing

Reald world example loading multiple libraries:

/**
 * Implementation of hook_init().
 */
function sooperthemes_basic_shortcodes_init() {
  module_load_include('module', 'jsdelivr');
  if (function_exists('jsdelivr_detect')) {
    if (jsdelivr_detect('animatecss') == FALSE) {
      jsdelivr_add_css('animatecss/3/animate.min.css');
    }
    if (jsdelivr_detect('fontawesome') == FALSE) {
      jsdelivr_add_css('fontawesome/4/css/font-awesome.min.css');
    }
    if (jsdelivr_detect('wow') == FALSE) {
      jsdelivr_add_js('wow@1');
    }
  }
}

Contributed by SooperThemes Premium Drupal Themes
Drupal Themes