renatomarinho/laravel-page-speed
The VinkiusLabs/LaravelPageSpeed package optimizes Laravel applications for performance, particularly focusing on API responses, health checks, and asset delivery. To get started:
composer require vinkiuslabs/laravel-page-speed
php artisan vendor:publish --provider="VinkiusLabs\LaravelPageSpeed\ServiceProvider" to customize settings (e.g., compression, caching).app/Http/Kernel.php:
protected $middleware = [
\VinkiusLabs\LaravelPageSpeed\Http\Middleware\ApiResponseCompression::class,
];
ApiHealthCheck middleware to monitor performance metrics:
Route::get('/health', \VinkiusLabs\LaravelPageSpeed\Http\Middleware\ApiHealthCheck::class);
Check the response for formatted byte sizes (now supporting TB via the new FormatsBytes trait).Response Optimization:
ApiResponseCompression. Configure thresholds in config/laravel-page-speed.php (e.g., min_bytes_to_compress).ElideAttributes to strip unnecessary HTML attributes (e.g., disabled, selected) from cached views:
use VinkiusLabs\LaravelPageSpeed\Traits\ElideAttributes;
class MyController {
use ElideAttributes;
public function index() {
return $this->elideAttributes(view('my.view'));
}
}
Performance Monitoring:
ApiPerformanceHeaders to add X-Response-Time and Content-Length headers. The formatBytes() method (now via FormatsBytes trait) supports B, KB, MB, GB, TB:
$size = 1500000000; // 1.5 GB
echo formatBytes($size); // Output: "1.5 GB"
ApiHealthCheck into routes to expose performance metrics:
Route::middleware(ApiHealthCheck::class)->group(function () {
Route::get('/metrics', [MetricsController::class, 'index']);
});
CSS/JS Optimization:
InlineCss to inline critical CSS in Octane/Swoole environments (note: static counter behavior is documented for these setups):
use VinkiusLabs\LaravelPageSpeed\Traits\InlineCss;
class MyController {
use InlineCss;
public function show() {
return $this->inlineCss(view('home'), ['styles.css']);
}
}
Comment Removal:
removeSingleLineCommentFromLine() (marked @deprecated). Use removeSingleLineCommentsFromContent() instead:
use VinkiusLabs\LaravelPageSpeed\Traits\RemoveComments;
$cleanedContent = removeSingleLineCommentsFromContent($phpFileContent);
protected $middlewareGroups = [
'web' => [
\VinkiusLabs\LaravelPageSpeed\Http\Middleware\ApiResponseCompression::class,
\VinkiusLabs\LaravelPageSpeed\Http\Middleware\ElideAttributes::class,
],
];
Deprecations:
removeSingleLineCommentFromLine() is deprecated. Update calls to removeSingleLineCommentsFromContent() to avoid runtime warnings.Regex Overmatching:
ElideAttributes trait’s regex for disabled/selected attributes was simplified. If you extended this trait, verify your custom logic still works.Octane/Swoole:
InlineCss uses a static counter for performance. In Octane, this may behave differently than in traditional PHP-FPM. Test thoroughly in your deployment environment.Byte Formatting:
formatBytes() now includes TB (previously capped at GB). If your app expects GB as the max, update display logic or config thresholds.min_bytes_to_compress in config. Set to 0 to compress all responses (not recommended for production).dd() on the ApiHealthCheck response to inspect raw data before formatting:
Route::get('/debug-health', function () {
$check = new \VinkiusLabs\LaravelPageSpeed\Http\Middleware\ApiHealthCheck();
dd($check->handle(new Request(), function () {}));
});
FormatsBytes to add new units (e.g., PB) or locales:
use VinkiusLabs\LaravelPageSpeed\Traits\FormatsBytes;
class MyFormatter {
use FormatsBytes;
protected $units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
}
ApiResponseCompression to add pre-compression hooks:
class CustomCompression extends ApiResponseCompression {
public function handle($request, Closure $next) {
$response = parent::handle($request, $next);
// Custom logic here
return $response;
}
}
FormatsBytes trait for unit tests:
$formatter = $this->getMockForTrait(FormatsBytes::class);
$formatter->method('formatBytes')->willReturn('Mocked Size');
Response::cache().ApiResponseCompression uses Laravel’s built-in compression. Ensure your server supports these algorithms (e.g., .htaccess for Apache).How can I help you explore Laravel packages today?