Type
magento2-module
N/A
magento2-module
MIT
None
None
None
None
None
The module moves javascripts to the bottom of the page.
composer require hryvinskyi/magento2-deferjs
bin/magento module:enable Hryvinskyi_Base
bin/magento module:enable Hryvinskyi_DeferJs
bin/magento setup:upgrade
bin/magento module:enable Hryvinskyi_Base
bin/magento module:enable Hryvinskyi_DeferJs
bin/magento setup:upgrade
To get the access to the 'Defer JavaScripts' settings please go to Stores -> Configuration -> Hryvinskyi Extensions -> Defer JavaScripts and expand the General Settings section.
Enabled deferred javascript: Enable or disable the extension from here.
Disable move attribute: Enter the attribute that will block the move of the script to the bottom.
Enabled minify body scripts: Enable script minification.
Exclude by controllers from defer parsing of JavaScript.
Controller will be unaffected by defer js. Use like: [module]_[controller]_[action] (Example: cms_index_index)
Exclude by store paths from defer parsing of JavaScript.
Paths will be unaffected by defer js. Use like: /women/tops-women/jackets-women.html
Exclude by url pattern from defer parsing of JavaScript.
URL pattern can be a full action name or a request path. Wildcards are allowed. Like:
*cell-phones*
*cell-phones/nokia-2610-phone.html
customer_account_*
/customer/account/*
*?mode=list
Minification of moved javascripts
To add your validator:
Create Validator file app/code/Vendor/Module/Model/PassesValidator/Validators/YourValidator.php
:
<?php
declare(strict_types=1);
namespace Vendor\Module\Model\PassesValidator\Validators;
use Hryvinskyi\DeferJs\Model\PassesValidator\ValidatorInterface;
use Magento\Framework\App\Response\Http;
/**
* Class YourValidator
*/
class YourValidator implements ValidatorInterface
{
/**
* Validator function, handle javascript or not
*
* @param string $script
* @param Http $http
*
* @return bool
*/
public function validate(string $script, Http $http): bool
{
// Your code validate
}
}
Create Dependency Injection file app/code/Vendor/Module/etc/frontend/di.xml
:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="deferJsPassesValidators">
<arguments>
<argument name="entityTypes" xsi:type="array">
<item name="yourValidationName"
xsi:type="object">Vendor\Module\Model\PassesValidator\Validators\YourValidator</item>
</argument>
</arguments>
</virtualType>
</config>
Run command:
php bin/magento setup:di:compile