Skip to content

Commit

Permalink
load critical css generator only when required #164
Browse files Browse the repository at this point in the history
  • Loading branch information
tbela99 committed Jul 2, 2022
1 parent 6823eac commit ac58ae3
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions helpers/CSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ public function preProcessHTML(array $options, $html)
return true;
}));



if (empty($data['dimensions'])) {

// all breakpoints processed
Expand All @@ -111,38 +109,35 @@ public function preProcessHTML(array $options, $html)
}
} else {

$hash = $root.md5(json_encode($matched));
$hash = $root . md5(json_encode($matched));

if (!empty($matched)) {

$cssParser = new Parser('', ['capture_errors' => false]);

if (!is_file($hash.$css_min.'.css')) {
if (!is_file($hash . $css_min . '.css')) {

foreach (array_reverse($matched) as $dimension) {

$file = $root.$dimension.$css_min.'.css';
$file = $root . $dimension . $css_min . '.css';

if (is_file($file)) {

$cssParser->append($file);
}
}

file_put_contents($hash.'.css', $cssParser);
file_put_contents($hash.'.min.css', (new Renderer(['compress' => true]))->renderAst($cssParser));
file_put_contents($hash . '.css', $cssParser);
file_put_contents($hash . '.min.css', (new Renderer(['compress' => true]))->renderAst($cssParser));
}

if (is_file($hash.$css_min.'.css')) {
if (is_file($hash . $css_min . '.css')) {

$html = preg_replace('#<head>#i', '<style data-ignore="true">'.file_get_contents($hash.$css_min.'.css').'</style>'."\n", $html, 1);
$html = preg_replace('#<head>#i', '<style data-ignore="true">' . file_get_contents($hash . $css_min . '.css') . '</style>' . "\n", $html, 1);

// $replace .= '<style data-ignore="true">'.file_get_contents($hash.$css_min.'.css').'</style>'."\n";
}
}
}

if (!empty($matched)) {

$dt = new \DateTime();
$dt->modify('+40s');
Expand All @@ -158,7 +153,7 @@ public function preProcessHTML(array $options, $html)
$profiler = \JProfiler::getInstance('Application');
$profiler->mark('criticalCssSetup');

$matched = array_values(array_filter($matched, function ($dimension) use($data) {
$matched = array_values(array_filter($matched, function ($dimension) use ($data) {

$dimension = intval($dimension);

Expand All @@ -183,6 +178,7 @@ public function preProcessHTML(array $options, $html)
$replace .= $script . "\n";
}


if (!empty($replace)) {

if (preg_match('#(<meta\s*charset=[^>]+>)#', $html, $match)) {
Expand Down Expand Up @@ -673,7 +669,7 @@ public function processHTML(array $options, $html)
* @throws \Exception
* @since 3.0
*/
public function afterInitialise(array $options)
public function afterInitialise(array $options)
{

if ($_SERVER['REQUEST_METHOD'] == 'POST' && $_SERVER['REQUEST_URI'] == GZipHelper::CRITICAL_PATH_URL && isset($_SERVER['HTTP_X_SIGNATURE'])) {
Expand Down Expand Up @@ -800,7 +796,7 @@ public function afterInitialise(array $options)
* @throws Parser\SyntaxError
* @since version
*/
public function parseBackgroundImages(Element $headStyle, array $options = [])
public function parseBackgroundImages(Element $headStyle, array $options = [])
{

if (empty($options['imagecssresize']) || empty($options['css_sizes'])) {
Expand Down

0 comments on commit ac58ae3

Please sign in to comment.