chore(bootstrap.php): separately minify php when files are requested
This commit is contained in:
parent
2b22d10b46
commit
320f5e53db
6 changed files with 259 additions and 14 deletions
|
@ -1,6 +1,37 @@
|
|||
<?php
|
||||
|
||||
define('PROJECT_ROOT', realpath(__DIR__.'/..'));
|
||||
define('PROJECT_ROOT', realpath(__DIR__ . '/..'));
|
||||
|
||||
require_once PROJECT_ROOT.'/vendor/autoload.php';
|
||||
require_once PROJECT_ROOT.'/app/Utils/init.php';
|
||||
require_once PROJECT_ROOT . '/vendor/autoload.php';
|
||||
require_once PROJECT_ROOT . '/app/Utils/init.php';
|
||||
|
||||
use voku\helper\HtmlMin;
|
||||
|
||||
$requestUri = $_SERVER['REQUEST_URI'] ?? '';
|
||||
$acceptHeader = $_SERVER['HTTP_ACCEPT'] ?? '';
|
||||
$isApi = str_starts_with($requestUri, '/api/');
|
||||
$isHtml = stripos($acceptHeader, 'text/html') !== false || empty($acceptHeader);
|
||||
|
||||
if (!$isApi && $isHtml) {
|
||||
ob_start(function ($buffer) {
|
||||
if (stripos($buffer, '<body') !== false) {
|
||||
try {
|
||||
$minifier = new HtmlMin();
|
||||
$buffer = $minifier->minify($buffer);
|
||||
|
||||
if (stripos($buffer, '</body>') === false) {
|
||||
$buffer .= '</body>';
|
||||
}
|
||||
|
||||
if (stripos($buffer, '</html>') === false) {
|
||||
$buffer .= '</html>';
|
||||
}
|
||||
|
||||
} catch (Throwable $e) {
|
||||
error_log("HTML minification failed: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return $buffer;
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue