codeat3/laravel-page-speed
Minify and optimize Laravel HTML responses via middleware for faster pages. Includes whitespace collapsing, comment removal, inline CSS, DNS prefetch insertion, and more. Install with Composer, publish config, then enable selected middlewares.
The Laravel Page Speed package (codeat3/laravel-page-speed) optimizes frontend assets (CSS/JS) and implements performance best practices like lazy loading, critical CSS, and asset minification. To start:
composer require codeat3/laravel-page-speed
php artisan vendor:publish --provider="Codeat3\PageSpeed\PageSpeedServiceProvider" to generate config/page-speed.php.config/page-speed.php:
'enabled' => env('PAGE_SPEED_ENABLED', true),
'lazy_load' => true,
'critical_css' => true,
@pageSpeed directive in views to auto-optimize assets:
@pageSpeed
config/page-speed.php:
'excludes' => [
'admin/**',
'vendor/**',
],
'lazy_load' => [
'images' => true,
'videos' => true,
'iframes' => false,
],
PageSpeed::criticalCss() in controllers or Blade:
<style>{{ PageSpeed::criticalCss() }}</style>
@pageSpeedPreload):
// In a service provider:
PageSpeed::extend('preload', function ($view, $expr) {
return '<link rel="preload" href="' . $expr . '">';
});
PageSpeed::middleware();
'critical_css' => [
'queue' => true,
],
'critical_css' => [
'cache' => true,
'cache_key' => 'pagespeed_critical_css',
],
PageSpeed::flush() in tests to reset cached optimizations.buffer size in config or exclude problematic assets.loading="lazy" is not duplicated (e.g., by other packages). Use PageSpeed::shouldLazyLoad() to check.page_speed:generate-critical-css jobs in Laravel queues. Retry failed jobs manually if needed.'debug' => env('APP_DEBUG', false),
config/page-speed.php aren’t blocking critical assets.?page-speed=1 query strings).Codeat3\PageSpeed\Contracts\Optimizer to add new optimizations (e.g., font preloading).page-speed.optimized events for post-optimization logic:
Event::listen(PageSpeedOptimized::class, function ($event) {
// Log or process optimized assets
});
'lazy_load' => env('PAGE_SPEED_LAZY_LOAD', true),
generateCriticalCss() method now returns a CriticalCss object instead of a raw string. Use $criticalCss->html() to get the output.PageSpeed instance. Update custom middleware:
public function __construct(PageSpeed $pageSpeed) { ... }
PageSpeed::optimize() method is deprecated. Use PageSpeed::process() instead.How can I help you explore Laravel packages today?