Сайт больше не создаёт копии объединённых файлов стилей и скриптов после каждого обновления: если оформление не менялось, используется уже готовый файл. Место на сервере расходуется в несколько раз медленнее. Добавлена очистка объединённых файлов, которые страницы сайта не использовали дольше недели. Её можно сначала запустить в пробном режиме и посмотреть, сколько места освободится. Исправлена редкая ошибка, из-за которой при одновременных заходах посетитель мог получить недописанный файл стилей.
Состав платформы
Сжатие ресурсов
Сборка и сжатие JS, CSS и HTML.
Установка
Подключите пакет в корне проекта на SkeekS Платформе, затем примените миграции проекта.
composer require skeeks/yii2-assets-auto-compress
Зависимости
- yiisoft/yii2^2
- yiisoft/yii2-httpclient^2
- tedivm/jshrinkv1.6.6
- CssMin
skeeks/cssmin^1.0
Инструкция из репозитория
Automatically compile and merge files js + css + html in yii2 project.
This solution enables you to dynamically combine js and css files to optimize the html page. This allows you to improve the performance of google page speed.
This tool only works on real sites. On the local projects is not working!
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist skeeks/yii2-assets-auto-compress "*"
or add
"skeeks/yii2-assets-auto-compress": "*"
How to use
//App config
[
'bootstrap' => ['assetsAutoCompress'],
'components' =>
[
//....
'assetsAutoCompress' =>
[
'class' => '\skeeks\yii2\assetsAuto\AssetsAutoCompressComponent',
],
//....
]
]
//App config with all options
[
'bootstrap' => ['assetsAutoCompress'],
'components' => [
//....
'assetsAutoCompress' => [
'class' => '\skeeks\yii2\assetsAuto\AssetsAutoCompressComponent',
'enabled' => true,
'readFileTimeout' => 3, //Time in seconds for reading each asset file
'jsCompress' => true, //Enable minification js in html code
'jsCompressFlaggedComments' => true, //Cut comments during processing js
'cssCompress' => true, //Enable minification css in html code
'cssFileCompile' => true, //Turning association css files
'cssFileCompileByGroups' => false //Enables the compilation of files in groups rather than in a single file. Works only when the $cssFileCompile option is enabled
'cssFileRemouteCompile' => false, //Trying to get css files to which the specified path as the remote file, skchat him to her.
'cssFileCompress' => true, //Enable compression and processing before being stored in the css file
'cssFileBottom' => false, //Moving down the page css files
'cssFileBottomLoadOnJs' => false, //Transfer css file down the page and uploading them using js
'jsFileCompile' => true, //Turning association js files
'jsFileCompileByGroups' => false //Enables the compilation of files in groups rather than in a single file. Works only when the $jsFileCompile option is enabled
'jsFileRemouteCompile' => false, //Trying to get a js files to which the specified path as the remote file, skchat him to her.
'jsFileCompress' => true, //Enable compression and processing js before saving a file
'jsFileCompressFlaggedComments' => true, //Cut comments during processing js
'noIncludeJsFilesOnPjax' => true, //Do not connect the js files when all pjax requests when all pjax requests when enabled jsFileCompile
'noIncludeCssFilesOnPjax' => true, //Do not connect the css files when all pjax requests when all pjax requests when enabled cssFileCompile
'htmlFormatter' => [
//Enable compression html
'class' => 'skeeks\yii2\assetsAuto\formatters\html\TylerHtmlCompressor',
'extra' => false, //use more compact algorithm
'noComments' => true, //cut all the html comments
'maxNumberRows' => 50000, //The maximum number of rows that the formatter runs on
//or
'class' => 'skeeks\yii2\assetsAuto\formatters\html\MrclayHtmlCompressor',
//or any other your handler implements skeeks\yii2\assetsAuto\IFormatter interface
//or false
],
],
//....
],
];
Compiled files and cleanup
Compiled files are named by their content: assets/css-compress/<md5 of content>.css
and assets/js-compress/<md5 of content>.js, without ?v=. The set of source URLs
(they carry asset directory hashes and ?v=mtime) maps to a compiled file through a
small index entry assets/*-compress/.index/<key>. Republishing sources without
changing them (a deploy touch, a package reinstall) adds an index entry, not a
new copy of the file. Files and index entries are written to .tmp-* and renamed.
A page hit refreshes the mtime of its index entry and compiled file at most once per
usageTouchInterval (1 hour). That mtime is the usage mark for cleanup; atime and
ctime are not used (backups and chown -R change them).
CompiledFilesCleaner removes files no page has used for days (7) days:
expired index entries, compiled files no live entry points to and that are older
than the retention, and .tmp-<16 hex> files older than an hour. Files of the
previous naming scheme have no index entry; they are removed only when the retention
has also passed since the switch (.index/.since), so HTML rendered before the
upgrade keeps its files. Without .since no compiled file is removed (expired index
entries and stale temporary files still are). Entries with other names are left
alone. It reads directory listings, stat() and the small index entries, never the
compiled content. If a directory or a live index entry cannot be read, no compiled
file of that directory is removed and the run reports an error.
Pages refresh usage marks and store files under a shared flock on
.index/.lock; cleanup re-checks the mark and unlinks under an exclusive one. A page
that marked a file in time keeps it; a page that comes too late finds it gone and
compiles again instead of recreating it empty. Only the hourly mark refresh and a
store take the lock; an ordinary hit does not, and only when its mark is known to be
fresh (a vanished index entry is a miss). If the lock cannot be taken, the page uses
its source files (with a warning in the log); if touch() is refused (a file of
another owner), the store rewrites the file, which gives it a fresh mark. The
compiled file is marked before its index entry, so a fresh entry always points to a
fresh file.
getSettingsHash() is part of the index key and lists the options that change the
compiled content (and the resolved webroot). A subclass adding such an option must
extend it.
//Console config
'controllerMap' => [
'assets-auto-compress' => [
'class' => \skeeks\yii2\assetsAuto\console\CleanupController::class,
'assetsDirs' => ['@frontend/web/assets'],
],
],
php yii assets-auto-compress/cleanup --dry-run
php yii assets-auto-compress/cleanup --days=7
A removed compiled file is rebuilt by the next page that renders it; a direct request
to it from an old HTML page gets 404, which is why the retention counts from the last use.
skeeks/cms-assets-auto-compress runs the cleanup daily as a cms-job job.
Tests: php tests/compiled-files.php vendor/autoload.php.
Links
Demo (view source code)
Screenshot
SkeekS CMS (Yii2) — fast, simple, effective!
skeeks.com | cms.skeeks.com
CSS: отказ от обязательного mrclay/minify
CSS-обработка перенесена из mrclay/minify 3.0.14 в отдельную папку нашего пакета без изменения алгоритмов. Это убирает обязательную цепочку к abandoned-пакету marcusschwarz/lesserphp, который не нужен для обработки CSS. Лицензия и авторство сохранены. Причины, состав, ограничения, обновление сайтов и будущий план перехода на matthiasmullie/minify описаны в документации.
Важно: старый альтернативный MrclayHtmlCompressor требует явной установки mrclay/minify; основной TylerHtmlCompressor этой зависимости не требует.
Поддержка
Вопросы по установке и работе решения можно задать командой SkeekS или завести обращение в репозитории.
Обновления
Исправлена ошибка, из-за которой при включённом объединении CSS-файлов часть оформления сайта пропадала: отступы, размеры и высота полей, заданные расчётом, переставали работать. Внешний вид сайтов с выключенным объединением CSS не меняется.
Убрана обязательная зависимость mrclay/minify, которая подключала неиспользуемый LESS-компилятор marcusschwarz/lesserphp, помеченный Composer как abandoned.
Только используемые четыре CSS-класса из mrclay/minify 3.0.14 перенесены в отдельную папку src/vendor/mrclay и собственное пространство имён. Алгоритмы сохранены; добавлены исходная BSD-3-Clause лицензия, сведения об авторстве, версии и контрольные суммы исходников.
Совместимость:
- Обработка CSS, основной HTML-форматтер и JavaScript сохраняют прежнее поведение.
- Необязательный MrclayHtmlCompressor требует явной установки mrclay/minify либо замены на TylerHtmlCompressor. При отсутствии зависимости возвращается понятная ошибка конфигурации.
- matthiasmullie/minify отмечен в документации как будущий отдельный этап и пока не подключён.
Проверки: PHP lint, Composer validate, 12 автономных CSS-сценариев, 144 побайтовых сравнения со старым кодом, включая реальные CSS главной и товара plitka-lab.ru. Проверены холодный и готовый кеш настоящего компонента с заблокированной загрузкой старой библиотеки, дополнительное сжатие в обоих режимах и совместимость необязательного HTML-адаптера. На боевом сайте главная, товар и их CSS возвращают HTTP 200. Визуальная проверка браузером не выполнена из-за ошибки запуска среды.
При обновлении сайтов нужно обновить пакет и зависимости через Composer. Простая загрузка файлов в vendor не изменяет composer.lock. Старые mrclay/minify и marcusschwarz/lesserphp удалятся только если больше не нужны другим зависимостям; после обновления проверить composer why и composer audit.
Ускорено сжатие многострочных HTML-страниц без изменения результата форматирования.
- Устранено копирование всего оставшегося документа при обработке каждой строки: используется смещение чтения.
- Сохранены обработка pre/textarea, комментариев, дополнительные параметры сжатия и статистика.
- Настройки приложения менять не требуется; изменение действует при использовании TylerHtmlCompressor.
Проверки: PHP 8.2, 14 регрессионных проверок и 1600 побайтных сравнений с предыдущей реализацией. На тестовом документе из 20 000 строк время алгоритма снизилось с 619 до 16 мс; это микробенчмарк, а не коэффициент ускорения всего сайта. Также выполнена проверка на работающем магазине.
- Пропуск повторной минификации файлов *.min.js при сборке общего JavaScript-бандла.
- Обычные JavaScript-файлы продолжают обрабатываться JSHrink.
- Устранён тайм-аут сборки больших готовых vendor-бандлов.





