livewire/blaze
Blaze speeds up Laravel Blade anonymous components by compiling templates into optimized PHP functions. Drop-in replacement with no code changes. Optional memoization and folding for extra gains. Enable via @blaze directive or optimize directories.
composer require livewire/blaze:^1.0
@blaze directive to any anonymous Blade component:
@blaze
<button>{{ $slot }}</button>
php artisan view:clear
Optimize a frequently rendered component like a button or icon:
@blaze
@props(['type' => 'button'])
<button {{ $attributes->merge(['type' => $type]) }}>
{{ $slot }}
</button>
Benchmark::dd() or Xdebug.@blaze directives before optimizing entire directories.Incremental Adoption:
@blaze on high-impact components (e.g., buttons, modals, cards).Blaze::optimize() in AppServiceProvider.Blaze::optimize()->in(resource_path('views/components/ui'));
Strategy Selection:
@blaze(memo: true)): Apply to static components (icons, avatars) rendered repeatedly.@blaze(fold: true)): Reserve for fully static components (e.g., decorative elements).Directory Optimization:
Blaze::optimize()
->in(resource_path('views/components/buttons'), memo: true)
->in(resource_path('views/components/icons'), fold: true)
->in(resource_path('views/components/legacy'), compile: false);
@entangle, $wire:model).$__env->shared('key') instead of relying on View::share().Blaze::shouldCompile() in tests to mock Blaze behavior:
Blaze::shouldCompile(false); // Disable Blaze for tests
@if(app()->environment('production'))
@blaze
@endif
<x-component> tags).@blaze(debug: true) for development).Global State in Folded Components:
auth(), session(), or now() will break.Dynamic Props in Folded Components:
@props receive dynamic values (e.g., :color="$dynamicValue").Function Compiler (@blaze) or refactor to static props.Slots with Dynamic Content:
@if) may not fold correctly.unsafe or avoid folding:
@blaze(fold: true)
@slot('dynamic', unsafe: true)
View Composers/Creators:
View::composer() and View::creator().@props or $__env->shared().Cross-Boundary @aware:
storage/framework/views for Blaze-generated PHP files. Errors often appear as BlazeException with hints.Benchmark::dd() to isolate slow components:
use Livewire\Benchmark;
Benchmark::dd(fn() => view('component'));
Memo::key('component', ['prop' => 'value']).Cache Invalidation:
php artisan view:clear
php artisan cache:clear
Folding Whitelisting:
@stack directives.@once blocks.@foreach with dynamic conditions).Environment-Specific Behavior:
local environment by default (set BLEZE_FOLD=false in .env to override).Custom Compilers:
Livewire\Blaze\Compilers\Compiler to add support for custom Blade directives or syntax.Memoization Backends:
Memo implementation by binding a custom Livewire\Blaze\Memo instance in the service provider.Folding Rules:
Livewire\Blaze\Folding\FoldingAnalyzer to add/remove folding restrictions.Directive Macros:
@blaze arguments:Blaze::macro('debug', function () {
return function ($view) {
// Custom logic
};
});
Usage:
@blaze(debug: true)
How can I help you explore Laravel packages today?