Skip to content

Instantly share code, notes, and snippets.

@iTeeLion
Last active December 23, 2020 14:47
Show Gist options
  • Save iTeeLion/696a4322e76936c91c71e5478e1552dc to your computer and use it in GitHub Desktop.
Save iTeeLion/696a4322e76936c91c71e5478e1552dc to your computer and use it in GitHub Desktop.
Bitrix google pagespeed optimization class / PHP class для bitrix, вырезает все системные JS и CSS на которые жалуется google pagespeed
<?php
$eventManager = EventManager::getInstance();
$eventManager->addEventHandler('main', 'OnEndBufferContent', ['\\App\\Event\\Optimization', 'deleteKernelJs']);
$eventManager->addEventHandler('main', 'OnEndBufferContent', ['\\App\\Event\\Optimization', 'deleteKernelCss']);
/** class **/
namespace App\Event;
class Optimization
{
public function deleteKernelJs(&$content)
{
global $USER, $APPLICATION;
if ((is_object($USER) && $USER->IsAuthorized()) || strpos($APPLICATION->GetCurDir(), '/bitrix/') !== false) {
return;
}
if ($APPLICATION->GetProperty('save_kernel') === 'Y') {
return;
}
$arPatternsToRemove = [
'/<script.+?src=".+?kernel_main\/kernel_main\.js\?\d+"><\/script\>/',
'/<script.+?src=".+?bitrix\/js\/main\/core\/core[^"]+"><\/script\>/',
'/<script.+?>BX\.(setCSSList|setJSList)\(\[.+?\]\).*?<\/script>/',
'/<script.+?>if\(\!window\.BX\)window\.BX.+?<\/script>/',
'/<script[^>]+?>\(window\.BX\|\|top\.BX\)\.message[^<]+<\/script>/',
];
$content = preg_replace($arPatternsToRemove, "", $content);
$content = preg_replace("/\n{2,}/", "\n\n", $content);
}
public function deleteKernelCss(&$content)
{
global $USER, $APPLICATION;
if ((is_object($USER) && $USER->IsAuthorized()) || strpos($APPLICATION->GetCurDir(), '/bitrix/') !== false) {
return;
}
if ($APPLICATION->GetProperty('save_kernel') === 'Y') {
return;
}
$arPatternsToRemove = [
'/<link.+?href=".+?kernel_main\/kernel_main\.css\?\d+"[^>]+>/',
'/<link.+?href=".+?bitrix\/js\/main\/core\/css\/core[^"]+"[^>]+>/',
'/<link.+?href=".+?bitrix\/templates\/[\w\d_-]+\/styles.css[^"]+"[^>]+>/',
'/<link.+?href=".+?bitrix\/templates\/[\w\d_-]+\/template_styles.css[^"]+"[^>]+>/',
];
$content = preg_replace($arPatternsToRemove, "", $content);
$content = preg_replace("/\n{2,}/", "\n\n", $content);
}
}
@iTeeLion
Copy link
Author

Как выключить автозагрузку js и css поставляемых с ядром битрикс

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment