Search code examples
javascripthtmlcssimpresspages

Only include scripts from used plugins in page


Is it possible to only include the scripts etc. that are actually used on the site/page in the HTML output shown to the world?

Loading the JS and CSS files for all installed plugins and the admin interface (like the following), slows down the page a lot:

var ip = {"baseUrl":"http:\/\/localhost\/ImpressPages\/","safeMode":false,"languageId":1...

Thanks Morten


Solution

  • Each plugin decides what assets should be included on each page load. Despite that you can create your own plugin that follows your own logic and removes some assets before page is rendered (I'd use ipBeforeController event for that). _1 means low priority to make sure your script is the last in a row.

    class Event
    {
        public static function ipBeforeController_1()
        {
            // Get all added JavaScript files
            $allJs = \Ip\ServiceLocator::pageAssets()->getJavascript();
    
            // ... your logic ...
    
            // Remove
            \Ip\ServiceLocator::pageAssets()->removeJavascript($fileName);
        }
    }