cancel
Showing results for 
Search instead for 
Did you mean: 

Need the possibility to load CSS asynchronously

Need the possibility to load CSS asynchronously

Feature request from mage2pro, posted on GitHub Oct 27, 2015

The decision whether a JavaScript component need be shown could be made when the layout is already built, so the component is unable to add its CSS resources by the standard way (\Magento\Framework\View\Page\Config::addPageAsset()). Is any standard possibility to load stylesheet asynchronously? (like require-css do).

(1)
7 Comments
apiuser
New Member
Status changed to: Investigating
 
apiuser
New Member

Comment from kandy, posted on GitHub Oct 27, 2015

You can use requirejs + data-mage-init / x-magento-init. See: http://devdocs.magento.com/guides/v2.0/javascript-dev-guide/javascript/js_init.html

apiuser
New Member

Comment from mage2pro, posted on GitHub Oct 27, 2015

I do not see any word about CSS there.

apiuser
New Member

Comment from kandy, posted on GitHub Oct 27, 2015

Sorry, i missed that it's about css. require-css will be good for this

apiuser
New Member

Comment from mage2pro, posted on GitHub Oct 27, 2015

My workaround solutions: https://mage2.pro/t/149

apiuser
New Member

Comment from guz-anton, posted on GitHub Nov 04, 2015

You can do it now in your project just with mentioned require-css. Steps to do:

  1. Imagine you are writing own Magento module
  2. You go to view/frontend folder under Your module.
  3. Create file requirejs-config.js with content:
    var config = {
    'map': {
    '*': {
    'css': 'Your_Module/js/require-css'
    // 'css': 'https://cdnjs.cloudflare.com/ajax/libs/require-css/0.1.8/css.js'
    // 'css': 'https://cdn.jsdelivr.net/require-css/0.1.8/css.min.js'
    }
    };
  4. Put source from https://cdnjs.cloudflare.com/ajax/libs/require-css/0.1.8/css.js to Your/Module/view/base/web/js/require-css.js
  5. Here you are! You can use it. For example:
    require([ 'css!mage/calendar' ], function() {
    // JS code is ready to use markup with styles
    });

And now Magento has no precedent to use css load in JS code. So we doesn't provide such plugin out-of-the-box.

apiuser
New Member

Comment from guz-anton, posted on GitHub Jul 13, 2016

Hi Dmitriy, We decide to make improvement in this area. We'll do some investigation under MAGETWO-55352.