承接 m2-boilerplate/module-critical-css 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

m2-boilerplate/module-critical-css

最新稳定版本:1.0.20

Composer 安装命令:

composer require m2-boilerplate/module-critical-css

包简介

Magento 2 module to automatically generate critical css with the addyosmani/critical npm package

README 文档

README

Magento 2 module to automatically generate critical css with critical

Features:

  • generate critical css with a simple command
  • Fallback critical css for "empty" pages
  • Add urls by creating a custom provider

Installation

Install the critical binary. Install instructions can be found on the critical website. Only versions >=2.0.3 are supported.

Add this extension to your Magento installation with Composer:

composer require m2-boilerplate/module-critical-css

Usage

Configuration

The critical css feature needs to be enabled (available in 2.3.4+):

bin/magento config:set dev/css/use_css_critical_path 1

Features can be customised in Stores > Configuration > Developer > CSS.

Generate critical css

Run the following command

bin/magento m2bp:critical-css:generate

Afterwards you should find the the generated css in var/critical-css. The css will now be integrated automatically on your website.

Add additional URLs via a custom provider

The following example adds the magento contact page via a custom provider:

<?php

namespace Vendor\Module\Provider;

use Magento\Framework\App\Request\Http;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Store\Api\Data\StoreInterface;
use M2Boilerplate\CriticalCss\Provider\ProviderInterface;
  
class CustomProvider implements ProviderInterface
{
    const NAME = 'custom_example';

    /**
     * @var UrlInterface
     */
    protected $url;

    public function __construct(UrlInterface $url)
    {
        $this->url = $url;
    }


    public function getUrls(StoreInterface $store): array
    {
        return [
            'contact_index_index' => $this->url->getUrl('contact'),
        ];
    }

    public function getName(): string
    {
        return self::NAME;
    }

    public function isAvailable(): bool
    {
        return true;
    }

    public function getPriority(): int
    {
        return 1200;
    }

    public function getCssIdentifierForRequest(RequestInterface $request, LayoutInterface $layout): ?string
    {
        if ($request->getModuleName() !== 'contact' || !$request instanceof Http) {
            return null;
        }
        
        if ($request->getFullActionName('_') === 'contact_index_index') {
            return 'contact_index_index';
        }

        return null;
    }
}

Add the new provider via DI:

in your module´s etc/di.xml add the following:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="M2Boilerplate\CriticalCss\Provider\Container">
        <arguments>
            <argument name="providers" xsi:type="array">
                <item name="custom_example" xsi:type="object">Vendor\Module\Provider\CustomProvider</item>
            </argument>
        </arguments>
    </type>
</config>

License

See the LICENSE file for license info (it's the MIT license).

统计信息

  • 总下载量: 75.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 37
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 36
  • Watchers: 2
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-09-05