Skip to content

blackbird-agency/external-resources-loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

External resources loader   Latest Stable Version License: MIT

This tiny Magento 2 module will allow you to easily lazy load scripts and styles by url.

Installation

composer require blackbird/external-resources-loader
php bin/magento setup:upgrade

Usage

Load script :

blackbird.loadExternalResource('https://cdn.jsdelivr.net/npm/@splidejs/[email protected]/dist/js/splide.min.js')
	.then(() => /* code which is executed after the lib has loaded */)

Load style :

blackbird.loadExternalResource('https://cdn.jsdelivr.net/npm/@splidejs/[email protected]/dist/css/splide.min.css')
	.then(() => /* code which is executed after the style has loaded */)

Load multiple related scripts and styles :

Promise.all([
    blackbird.loadExternalResource('https://cdn.jsdelivr.net/npm/@splidejs/[email protected]/dist/js/splide.min.js'),
    blackbird.loadExternalResource('https://cdn.jsdelivr.net/npm/@splidejs/[email protected]/dist/css/splide.min.css')
]).then(() => /* code which is executed after the style and the lib has loaded */)

Troubleshooting

The loadExternalResource method will load a script or a style depending on the url extension. If the url doesn't end by .js or .css, you could lazy load resource using one of these 2 methods :

  • loadExternalScript : load resource from a url that provides javascript content
  • loadExternalStyle : load resource from a url that provides css content

For instance :

blackbird.loadExternalScript("<?= $block->getViewFileUrl('js/my-script') ?>")