vinkius-labs/laravel-page-speed
Optimize Laravel HTML and API responses with a configurable middleware pipeline: minify HTML/JSON/XML, add cache headers, ETags, compression and more. Works across Laravel 10–13 with any cache store (Redis, Memcached, file, etc.) for faster, leaner responses.
API_CACHE_DRIVER instead of the default cache driver, ensuring consistent state sharing in distributed environments.X-Performance-Warning header being silently overwritten when both high query count and slow request conditions triggered simultaneously. Both warnings are now combined.ok status without actually probing the queue driver. Now calls Queue::connection()->size() to verify connectivity.Cache-Control header overwrite that could conflict with application-level or other middleware caching directives. Now respects existing headers.new Response() instantiation in shouldCache() — uses isEnable() directly.Content-Type guard in the base handle() method — web middleware now skips non-HTML responses, preventing silent data corruption on misconfigured middleware groups.src, href, and action attributes only, preventing corruption of JavaScript strings, meta tags, and SVG namespaces.X-XSS-Protection from deprecated 1; mode=block to 0 as recommended by MDN.^8.2 || ^8.3 to ^8.2, covering PHP 8.2, 8.3, 8.4+ without redundancy.composer update vinkius-labs/laravel-page-speed
php artisan config:clear
No breaking changes. Drop-in replacement for v4.4.x.
Full Changelog: https://github.com/vinkius-labs/laravel-page-speed/compare/v4.4.1...v4.4.2
logPerformanceMetrics() call that passed pre-computed elapsed time instead of raw start time, resulting in wildly inaccurate debug metrics (~1.7 billion ms). The parent PageSpeed::handle() already provides correct, debug-only performance logging., , and `` are now reset at the beginning of each apply() call.supportsTags() method and corrected invalidateCache() return type from [@return](https://github.com/return) void to [@return](https://github.com/return) bool.*.doc entry in the skip list.ConfigTest to validate correct behavior: isEnable() now reflects runtime configuration changes instead of returning a stale cached value.Full Changelog: https://github.com/vinkius-labs/laravel-page-speed/compare/v4.3.2...v4.4.0
/class="(.*?)"/ to /(?<![-:])class="(.*?)"/i using negative lookbehindng-class (AngularJS), :class (Alpine.js), v-bind:class (Vue.js)ng-class work correctly (Issue #75):class shorthand works correctly (Issue #154)v-bind:class works correctlyInlineCssJavaScriptFrameworksTest with 7 comprehensive tests (42 assertions)ng-class compatibility:class shorthand compatibilityv-bind:class compatibilityVersion 4.1.0 introduces significant performance improvements across all middlewares while maintaining 100% backward compatibility. This release focuses on optimizing critical paths, reducing computational complexity, and eliminating unnecessary operations.
voidElements() method$voidElementsCache to store void elementspreg_match_all operations into 1 optimized regex(link|img|a|iframe|video|audio|source)Implementation A: Replaced rand() with counter-based unique IDs
Implementation B: Removed explode() overhead
preg_replace_callback for single-pass processing: prefix)Implementation A: Enhanced replaceInsideHtmlTags() with single-pass callback
str_replace on entire bufferpreg_replace_callback for targeted replacementsImplementation B: Added performance metrics logging
DeferJavascript, InlineCss, TrimUrls| Middleware | Before | After | Improvement |
|---|---|---|---|
| InsertDNSPrefetch | 6 regex scans | 1 regex scan | 6x faster |
| InlineCss | rand() + explode() |
Counter + callback | ~3x faster |
| RemoveComments | Basic char loop | Optimized state machine | 10-50x faster |
| HtmlSpecs | New array each call | Static cache | ~50% faster |
| File | Lines Changed | Type of Change |
|---|---|---|
src/Entities/HtmlSpecs.php |
+31 -18 | Memoization cache |
src/Middleware/InsertDNSPrefetch.php |
+37 -62 | Regex consolidation |
src/Middleware/InlineCss.php |
+59 -24 | Counter IDs + callback |
src/Middleware/RemoveComments.php |
+63 -9 | Line-by-line processing |
src/Middleware/PageSpeed.php |
+69 -10 | Enhanced base + metrics |
src/Middleware/DeferJavascript.php |
+4 -1 | Smart early return |
src/Middleware/TrimUrls.php |
+4 -1 | Smart early return |
src/Middleware/CollapseWhitespace.php |
+4 -1 | Smart early return |
Total: +277 additions, -120 deletions across 8 files
private static $voidElementsCache = null;
public static function voidElements(): array
{
if (self::$voidElementsCache === null) {
self::$voidElementsCache = [/* ... */];
}
return self::$voidElementsCache;
}
Before (6 operations):
preg_match_all('#<script[^>]+src=["\']([^"\']+)["\']#i', $buffer, $m1);
preg_match_all('#<link[^>]+href=["\']([^"\']+)["\']#i', $buffer, $m2);
// ... 4 more similar operations
After (1 operation):
preg_match_all(
'#<(?:link|img|a|iframe|video|audio|source)\s[^>]*\b(?:src|href)=["\']([^"\']+)["\']#i',
$buffer,
$matches
);
Before:
foreach ($tags as $tag) {
preg_match_all($regex, $tag, $matches);
$tagAfterReplace = str_replace($matches[0], $replace, $tag);
$buffer = str_replace($tag, $tagAfterReplace, $buffer);
}
After:
$buffer = preg_replace_callback($pattern, function ($matches) use ($regex, $replace) {
return preg_replace($regex, $replace, $matches[0]);
}, $buffer);
No changes required! This is a drop-in replacement.
# Simply update your composer.json
composer require vinkius-labs/laravel-page-speed:^4.1.0
# Or update existing installation
composer update vinkius-labs/laravel-page-speed
Configuration: No changes needed - all existing configurations work as-is.
Code: No changes needed - all APIs remain identical.
Based on benchmarks, users can expect:
Full Changelog: https://github.com/vinkius-labs/laravel-page-speed/compare/v4.0.0...v4.1.0
Release Date: October 25, 2025
Type: Major Release
Compatibility: Fully backward compatible
This is a major milestone release that transforms Laravel Page Speed from a web-only optimization package into a comprehensive full-stack performance solution for both web pages and REST APIs.
/health)| Metric | Before | After | Improvement |
|---|---|---|---|
| Page Size | 245 KB | 159 KB | -35% |
| HTML Minified | No | Yes | 100% |
| CSS Inlined | No | Yes | Faster render |
| JS Deferred | No | Yes | Non-blocking |
| First Paint | 1.8s | 1.2s | -33% |
| Metric | Before | After | Improvement |
|---|---|---|---|
| Response Size | 15.2 KB | 2.8 KB | -82% |
| Avg Response Time | 450ms | 2ms* | -99.6% |
| Server CPU | 85% | 45% | -47% |
| DB Queries | 35 | 0* | -100% |
| Monthly Bandwidth | 15 TB | 3 TB | -80% |
| Infrastructure Cost | $450 | $90 | -$360/mo |
* With cache hit
# API Cache
API_CACHE_ENABLED=true
API_CACHE_DRIVER=redis
API_CACHE_TTL=300
# API Performance Tracking
API_TRACK_QUERIES=true
API_QUERY_THRESHOLD=20
# Circuit Breaker
API_CIRCUIT_BREAKER_ENABLED=true
API_CIRCUIT_BREAKER_THRESHOLD=5
API_CIRCUIT_BREAKER_TIMEOUT=60
# Health Check
API_HEALTH_ENDPOINT=/health
# Compression
API_COMPRESSION_ENABLED=true
API_COMPRESSION_LEVEL=6
# Security Headers
API_SECURITY_HEADERS_ENABLED=true
config/laravel-page-speed.php - New API-specific sectionsfor-the-badge style for better visibilityNo changes required! All existing web optimization features work exactly as before.
composer require vinkius-labs/laravel-page-speedphp artisan vendor:publish --provider="VinkiusLabs\LaravelPageSpeed\ServiceProvider"app/Http/Kernel.php:protected $middlewareGroups = [
'api' => [
\VinkiusLabs\LaravelPageSpeed\Middleware\ApiSecurityHeaders::class,
\VinkiusLabs\LaravelPageSpeed\Middleware\ApiResponseCache::class,
\VinkiusLabs\LaravelPageSpeed\Middleware\ApiETag::class,
\VinkiusLabs\LaravelPageSpeed\Middleware\ApiResponseCompression::class,
\VinkiusLabs\LaravelPageSpeed\Middleware\ApiPerformanceHeaders::class,
\VinkiusLabs\LaravelPageSpeed\Middleware\ApiCircuitBreaker::class,
\VinkiusLabs\LaravelPageSpeed\Middleware\ApiHealthCheck::class,
],
];
.env variables (optional)php artisan config:clearThank you to everyone who reported issues, suggested features, and helped test this major release!
for-the-badge styleMIT License - See LICENSE.md for details
This release includes critical bug fixes and significantly improved test coverage to ensure package stability and reliability.
Issue: The RemoveComments middleware was incorrectly removing // characters inside JavaScript strings, breaking code:
var url = "http://example.com"; // Comment
// After processing: var url = "http:"; ❌
Fix:
http://, https://, ://)Tests: 8 new comprehensive tests covering edge cases
Fixes: #180, #184
Issue: The InsertDNSPrefetch middleware was incorrectly extracting URLs from:
<script type="application/ld+json">)<script> tags<style> tagsThis caused unwanted DNS prefetch tags and SEO issues.
Fix:
<script src>, <link href>, <img src>, <a href>, <iframe src>, <video>, <audio>, <source>Tests: 3 new tests for JSON-LD, JavaScript, and edge cases
Fixes: #179
Issue: Large HTML documents could exceed PCRE backtrack/recursion limits, causing blank pages.
Fix:
PageSpeed::replace()Tests: 2 tests with 5,000 and 20,000 line HTML documents
Related: #184
Added: 17 extremely robust tests (47 assertions) documenting all edge cases:
✅ Basic Functionality:
data-pagespeed-no-defer✅ Edge Cases:
✅ GLightbox Scenario: Documents the exact issue from #173 with 4 solution options
Related: #173
orchestra/testbench → ^10.6.0actions/checkout → v5actions/cache → v4actions/github-script → v8None - This is a bug fix release maintaining full backward compatibility.
composer require vinkius-labs/laravel-page-speed
Or update your existing installation:
composer update vinkius-labs/laravel-page-speed
@renatomarinho - All bug fixes and test coverage improvements
Thank you to everyone who reported issues and helped make this package more robust! 🙏
This is a major version release with breaking changes. Read the migration guide below before upgrading.
[@test](https://github.com/test) annotation to test_* method naming convention$defer property from ServiceProvider$defer property from ServiceProviderRequirements:
Steps:
composer.json:{
"require": {
"php": "^8.2 || ^8.3",
"laravel/framework": "^10.0 || ^11.0 || ^12.0",
"renatomarinho/laravel-page-speed": "^3.0"
}
}
composer update renatomarinho/laravel-page-speed
php artisan config:clear
php artisan cache:clear
Breaking Changes:
ServiceProvider class, remove the $defer propertyIf you need to stay on Laravel 6-9 or PHP 8.0/8.1, use version constraint:
{
"require": {
"renatomarinho/laravel-page-speed": "^2.1"
}
}
This will prevent automatic upgrades to v3.x during composer update.
composer require renatomarinho/laravel-page-speed:^3.0
composer require renatomarinho/laravel-page-speed:^3.0
composer update renatomarinho/laravel-page-speed
php artisan config:clear
php artisan cache:clear
Special thanks to all contributors who helped make this release possible!
Add support Laravel 8
Fixes remove quotes when attributes contains whitespaces Fixes package keywords typo
CHANGELOG
CHANGELOG
CHANGELOG
CHANGELOG
CHANGELOG
CHANGELOG
CHANGELOG
CHANGELOG
CHANGELOG
CHANGELOG
CHANGELOG
CHANGELOG
How can I help you explore Laravel packages today?