livewire/blaze
Blaze accelerates Laravel anonymous Blade components by compiling templates into optimized PHP functions, cutting 91–97% of rendering overhead with drop-in compatibility. Enable per component via @blaze or optimize directories, with optional memoization and folding.
Installation
composer require livewire/blaze:^1.0
No additional dependencies or complex setup required.
Enable for a Single Component
Add the @blaze directive to any Blade template:
@blaze
<div>
{{ $slot }}
</div>
This replaces the standard Blade rendering pipeline for this component.
First Use Case
Optimize a frequently rendered anonymous component (e.g., a dropdown, modal, or card) by wrapping it in @blaze. Test performance with:
php artisan blaze:benchmark
Compare before/after render times in your browser’s dev tools.
Selective Optimization
Use @blaze on components with high render frequency (e.g., tooltips, loading spinners, or repeated UI elements). Avoid over-optimizing rarely used templates.
Directory-Level Optimization
In your AppServiceProvider’s boot() method, register Blaze for entire directories:
use Livewire\Blaze\Facades\Blaze;
public function boot(): void
{
Blaze::optimizeDirectory(resource_path('views/components'));
}
This compiles all Blade files in the directory into optimized PHP functions.
Integration with Livewire Blaze works seamlessly with Livewire components. No changes needed—just wrap your Blade templates:
@blaze
<x-livewire-example />
Conditional Rendering
Combine with Blade’s @if directives for dynamic optimization:
@if(request()->wantsJson())
@blaze
<div>{{ $content }}</div>
@else
<div>{{ $content }}</div>
@endif
Memoization for Static Data Cache repeated renders of components with static data:
@blaze(memoize)
<div>{{ $staticData }}</div>
Configure memoization keys in config/blaze.php:
'memoization' => [
'enabled' => true,
'cache_store' => 'file',
],
Folding for Static HTML
Pre-render components into static HTML (requires php artisan blaze:fold):
@blaze(fold)
<div>Static content</div>
Useful for hero sections, footers, or documentation pages.
Hybrid Approach
Use @blaze for dynamic components and @once for static fallbacks:
@once
@blaze
<div>{{ $dynamicContent ?? 'Fallback' }}</div>
@endonce
Not All Blade Features Supported
@stack, @push, @prepend, or dynamic component registration (@component('dynamic-name')).storage/logs/blaze.log for unsupported directive warnings.Memoization Cache Invalidation
php artisan cache:clear
php artisan blaze:clear
Folding Limitations
{{ $user->name }}).@blaze(memoize) for semi-static cases.Livewire Component Conflicts
render() method doesn’t rely on Blade-specific behavior (e.g., @stack pushes).php artisan blaze:disable) to isolate issues.Development vs. Production
APP_DEBUG=true). Test optimizations in a staging environment or locally with APP_DEBUG=false.Benchmark Strategically
Use php artisan blaze:benchmark to measure improvements. Focus on components with:
Exclude Directories
Opt out of directory optimization in config/blaze.php:
'directories' => [
resource_path('views/components') => true,
resource_path('views/emails') => false, // Exclude emails
],
Custom Compilation Logic Extend Blaze’s compiler by publishing and modifying the config:
php artisan vendor:publish --tag=blaze-config
Override BlazeCompiler in app/Providers/BlazeServiceProvider.php:
public function register(): void
{
$this->app->singleton(BlazeCompiler::class, function () {
return new CustomBlazeCompiler();
});
}
Monitor Performance Track Blaze’s impact with Laravel’s query log or custom metrics:
// In AppServiceProvider
Blaze::optimized(function (string $component, float $duration) {
info("Blaze optimized {$component} in {$duration}ms");
});
Fallback for Unsupported Cases Use a helper to conditionally enable Blaze:
if (app()->environment('production') && !Blaze::isSupported($content)) {
return $content; // Fallback to standard Blade
}
return Blaze::render($content);
How can I help you explore Laravel packages today?