├── composer.json ├── registration.php ├── etc ├── module.xml └── frontend │ └── di.xml ├── README.md └── Plugin └── ResponsePlugin.php /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "goivvy/djs", 3 | "description": "Magento 2 Defer Javascript Loading Extension", 4 | "require": { 5 | "php": "7.0.2|7.0.4|~7.0.6|~7.1.0" 6 | }, 7 | "type": "magento2-module", 8 | "version": "1.0.0", 9 | "license": [ 10 | "OSL-3.0" 11 | ], 12 | "authors": [ 13 | { 14 | "name": "GOIVVY LLC", 15 | "email": "sales@goivvy.com", 16 | "homepage": "https://www.goivvy.com" 17 | } 18 | ], 19 | "autoload": { 20 | "files": [ 21 | "registration.php" 22 | ], 23 | "psr-4": { 24 | "Goivvy\\DJS\\": "" 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /registration.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | 16 | 17 | \Magento\Framework\Component\ComponentRegistrar::register( 18 | \Magento\Framework\Component\ComponentRegistrar::MODULE, 19 | 'Goivvy_DJS', 20 | __DIR__ 21 | ); 22 | -------------------------------------------------------------------------------- /etc/module.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DJS 2 | Magento 2 Defer Javascript Loading Extension 3 | 4 | 5 | This extension moves Javascript to the bottom of a Magento 2 frontend page. 6 | 7 | It attempts to solve render-block JS problem. 8 | 9 | Some screenshots of improving PageSpeed Score using this extension: 10 | 11 | https://www.goivvy.com/media/wysiwyg/defer-javascript-user-guide/pagespeed_desktop.jpg 12 | 13 | https://www.goivvy.com/media/wysiwyg/defer-javascript-user-guide/pagespeed_mobile.jpg 14 | 15 | GTMetrix: 16 | 17 | https://www.goivvy.com/media/wysiwyg/defer-javascript-user-guide/djs_gtmetrix.jpg 18 | 19 | Webpagetest.org: 20 | 21 | https://www.goivvy.com/media/wysiwyg/defer-javascript-user-guide/djs_webpagetest.jpg 22 | 23 | 24 | # Configuration 25 | 26 | It doesn't need configuration. 27 | 28 | Works automatically after installation. 29 | 30 | TTFB overhead is minimal. 31 | 32 | 33 | 34 | # Interested in speeding up Magento? 35 | 36 | See 30 tips to speed up Magento 2: 37 | 38 | https://www.goivvy.com/blog/speed-up-magento-2 39 | -------------------------------------------------------------------------------- /etc/frontend/di.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Plugin/ResponsePlugin.php: -------------------------------------------------------------------------------- 1 | 14 | */ 15 | 16 | namespace Goivvy\DJS\Plugin; 17 | 18 | class ResponsePlugin { 19 | public function aroundsendContent(\Zend\Http\PhpEnvironment\Response $response, callable $proceed){ 20 | $url = \Magento\Framework\App\ObjectManager::getInstance() 21 | ->get('Magento\Framework\UrlInterface'); 22 | if(preg_match('#checkout#',$url->getCurrentUrl())) return $proceed(); 23 | $content = $response->getContent(); 24 | preg_match_all('#()#is', $content, $matches); 25 | $js = ''; 26 | foreach ($matches[0] as $value) 27 | $js .= $value; 28 | $content = preg_replace('##is', '', $content); 29 | $content = preg_replace('##',$js.'',$content); 30 | $response->setContent($content); 31 | return $proceed(); 32 | } 33 | } 34 | --------------------------------------------------------------------------------