DeferCss.php in Advanced CSS/JS Aggregation 8.3
File
advagg_mod/src/Asset/DeferCss.php
View source
<?php
namespace Drupal\advagg_mod\Asset;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Component\Utility\Crypt;
class DeferCss {
protected $deferMethod;
protected $counter;
protected $external;
public function __construct(ConfigFactoryInterface $config_factory) {
$this->deferMethod = $config_factory
->get('advagg_mod.settings')
->get('css_defer_js_code');
$this->counter = $config_factory
->get('advagg.settings')
->get('global_counter');
$this->external = $config_factory
->get('advagg_mod.settings')
->get('css_defer_external');
}
public function defer($content) {
if ($this->external) {
$pattern = '/<link rel=["\']stylesheet["\'](.*)(href="[a-zA-Z0-9\\/_\\.\\-\\?\\:]*")(.*)\\/\\>/';
}
else {
$pattern = '/<link rel=["\']stylesheet["\'](.*)(href="\\/[a-zA-Z0-9][a-zA-Z0-9\\/_\\.\\-\\?]*")(.*)\\/\\>/';
}
$content = preg_replace_callback($pattern, [
$this,
'callback',
], $content);
if ($this->deferMethod === 0) {
$path = drupal_get_path('module', 'advagg_mod') . '/js/loadCSS.js';
if (!strpos($content, $path)) {
$path = Crypt::hashBase64($path . $this->counter);
}
$path = preg_quote($path, '/');
$pattern = "/<script src=['\"]\\/(.*{$path}.*)\\?.*['\"]>/";
$content = preg_replace_callback($pattern, [
$this,
'inlineScript',
], $content);
}
return $content;
}
protected function callback(array $matches) {
return "<link rel='preload' {$matches[1]} {$matches[2]} as='style' onload=\"this.rel='stylesheet'\" {$matches[3]}/><noscript>{$matches[0]}</noscript>";
}
protected function inlineScript(array $matches) {
$data = @file_get_contents($matches[1]);
return "<script>{$data}";
}
}
Classes
Name |
Description |
DeferCss |
Modify stylesheet links to defer them. May lead to Flash of unstyled content. |